title: threaded server in c
body: <p>I did not found an answer to my question before, nor did i know if that has an specific name for what i want to do, basically i have a program that runs a simulation, with graphical interface and calculations in the background, and i want to control it through commands with a tcp basic server/client but this means that i have to incorporate the server function inside the controlling function, so i made my server inside a thread and i am running something in the main function, while i call the server function in a thread just for testing, but i cannot send anything to the server, my client application does not get a response, and the server does not receive anything... the code is messy because i am testing a lot of stuff in it, and the identation is messy too...</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;pthread.h&gt;
  
  int test= 0;
  
  void threadServer(){
      int sockfd, newsockfd, portno, clilen;
      char buffer[256];
      struct sockaddr_in serv_addr, cli_addr;
      int  n, c;
  
      /* First call to socket() function */
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
      {
          perror("ERROR opening socket");
          exit(1);
      }
      /* Initialize socket structure */
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      portno = 5001;
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = INADDR_ANY;
      serv_addr.sin_port = htons(portno);
  
      /* Now bind the host address using bind() call.*/
      if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
                            sizeof(serv_addr)) &lt; 0)
      {
           perror("ERROR on binding");
           exit(1);
      }
  
      /* Now start listening for the clients, here process will
      * go in sleep mode and will wait for the incoming connection
      */
      listen(sockfd,5);
      clilen = sizeof(cli_addr);
  while(c){
      /* Accept actual connection from the client */
      newsockfd = accept(sockfd, (struct sockaddr *)&amp;cli_addr, 
                                  &amp;clilen);
      if (newsockfd &lt; 0) 
      {
          perror("ERROR on accept");
          exit(1);
      }
      /* If connection is established then start communicating */
      bzero(buffer,256);
      n = read( newsockfd,buffer,255 );
      if (n &lt; 0)
      {
          perror("ERROR reading from socket");
          exit(1);
      }
  //if(buffer[0]==t){
  //c=0;
  //exit(1);
  //}else{
  //c=1;
  //}
      printf("Here is the message: %s\n",buffer);
  
      /* Write a response to the client */
      n = write(newsockfd,"I got your message",18);
      if (n &lt; 0)
      {
          perror("ERROR writing to socket");
          exit(1);
      }
      test =1;
  }
      //return 0; 
  }
  
  void main(){
  
  pthread_t threads[1];
     int rc;
     long t;
     char k;
        printf("In main: creating thread %ld\n", t);
        rc = pthread_create(&amp;threads[t], NULL, threadServer, (void *)t);
        if(rc){
           printf("ERROR; return code from pthread_create() is %d\n", rc);
           exit(-1);
       }
      while(1){
      printf("type any key:\n");
      scanf("%c",&amp;k);
      printf("you typed %c\n", k);
      printf("testing server thread : %d\n", test);
      }
     /* Last thing that main() should do */
     pthread_exit(NULL);
  
  }
  </code></pre>