title: Multithreaded client-server socket programming in C
body: <p>I am implementing a Multi-threaded client-server socket programming in C on the same machine with the same IP-Address but with different ports for Client and Server. I have implemented it using pthread concepts in C environment. But I can see only my Client thread is running whereas my server thread has stopped once it reached 'accept()' routine. 
  I am wondering what may be the problem. If any one can find out where I am making a mistake then it would be really helpful</p>
  
  <p>My CLIENT code looks like this:</p>
  
  <pre><code>void *client_connect(void *arg)
  {
  
     int client_socket;
     struct sockaddr_in Serv_Addr;
     struct sockaddr_in Client_Addr;
     int addrlen=sizeof(Client_Addr);
  
     char send_buffer_client[] = {"server message"};
     char recv_buffer_client[1024];
     int nbytes;
  
     client_socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
     if (client_socket &lt; 0) ;
  
     memset((char *)&amp;Serv_Addr, 0, sizeof(Serv_Addr));
     Serv_Addr.sin_family = AF_INET;
     Serv_Addr.sin_len = sizeof(Serv_Addr);
     Serv_Addr.sin_addr.s_addr = inet_addr("1.2.3.4");
     Serv_Addr.sin_port = 9999;
     memset((char *)&amp;Client_Addr, 0, sizeof(Client_Addr));
     Client_Addr.sin_family = AF_INET;
     Client_Addr.sin_len = sizeof(Client_Addr);
     Client_Addr.sin_addr.s_addr = inet_addr("1.2.3.4");
     Client_Addr.sin_port = 5555;
  
     lwip_connect(client_socket, (struct sockaddr *)&amp;Serv_Addr, sizeof(Serv_Addr));
  
     while (1) {
  
                 do{
                     nbytes = lwip_recv(client_socket, recv_buffer_client, sizeof(recv_buffer_client),0);
                     if (nbytes&gt;0) lwip_send(client_socket, send_buffer_client, sizeof(send_buffer_client), 0);
  
                     printf("server message = %s\n", recv_buffer_client);
                 }  while (nbytes&gt;0);
  
                 sleep(10);
  
         }
         lwip_close(client_socket);
  }
  </code></pre>
  
  <p>My SERVER code:</p>
  
  <pre><code>void *server_connect(void *arg)
  {
  
     int server_socket;
     struct sockaddr_in Serv_Addr;
     struct sockaddr_in Client_Addr;
     int addrlen=sizeof(Client_Addr);
     int clientfd;
     char send_buffer[] = {"Server message"};
     char recv_buffer[1024];
     int nbytes_server, client_length;
  
     server_socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
     if (server_socket &lt; 0)
     printf("could not create server socket");
     else
     printf("created SERVER socket");
  
     memset((char *)&amp;Serv_Addr, 0, sizeof(Serv_Addr));
     Serv_Addr.sin_family = AF_INET;
     Serv_Addr.sin_len = sizeof(Serv_Addr);
     Serv_Addr.sin_addr.s_addr = inet_addr("1.2.3.4");
     Serv_Addr.sin_port = 9999;
  
  
     client_length = sizeof(Client_Addr);
     if (lwip_bind(server_socket, (struct sockaddr *)&amp;Serv_Addr, sizeof(Serv_Addr)) &lt; 0) {           
             printf("could not BIND");
     }
  
     if ( lwip_listen(server_socket, 20) != 0 ){
              printf("could not BIND");
     }
     while (1) {
                  lwip_accept(server_socket, (struct sockaddr*)&amp;Client_Addr, &amp;client_length);                
  
                 do{
                  nbytes_server = lwip_recv(server_socket, recv_buffer, sizeof(recv_buffer),0);
                  if (nbytes_server&gt;0){lwip_send(server_socket, send_buffer, sizeof(send_buffer), 0);}
  
                  printf("client message = %s\n", recv_buffer);
                  }while(nbytes_server&gt;0); 
                  sleepms(10);
         }
         lwip_close(server_socket);
  }
  
  void main(void)
  {
      pthread_t  client_thread;
      pthread_t  server_thread;
  
      pthread_create(&amp;server_thread, NULL, server_connect, NULL);
      pthread_create(&amp;client_thread, NULL, client_connect, NULL);
  
      while(1){
          sleepms(1);
          }
  }
  </code></pre>
  
  <p>Please let me know if I am doing it the wrong way</p>
  
  <p>Regards
  Deb</p>