Multithreaded client-server socket programming in C

Devb picture Devb · May 4, 2011 · Viewed 16.4k times · Source

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

My CLIENT code looks like this:

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 < 0) ;

   memset((char *)&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 *)&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 *)&Serv_Addr, sizeof(Serv_Addr));

   while (1) {

               do{
                   nbytes = lwip_recv(client_socket, recv_buffer_client, sizeof(recv_buffer_client),0);
                   if (nbytes>0) lwip_send(client_socket, send_buffer_client, sizeof(send_buffer_client), 0);

                   printf("server message = %s\n", recv_buffer_client);
               }  while (nbytes>0);

               sleep(10);

       }
       lwip_close(client_socket);
}

My SERVER 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 < 0)
   printf("could not create server socket");
   else
   printf("created SERVER socket");

   memset((char *)&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 *)&Serv_Addr, sizeof(Serv_Addr)) < 0) {           
           printf("could not BIND");
   }

   if ( lwip_listen(server_socket, 20) != 0 ){
            printf("could not BIND");
   }
   while (1) {
                lwip_accept(server_socket, (struct sockaddr*)&Client_Addr, &client_length);                

               do{
                nbytes_server = lwip_recv(server_socket, recv_buffer, sizeof(recv_buffer),0);
                if (nbytes_server>0){lwip_send(server_socket, send_buffer, sizeof(send_buffer), 0);}

                printf("client message = %s\n", recv_buffer);
                }while(nbytes_server>0); 
                sleepms(10);
       }
       lwip_close(server_socket);
}

void main(void)
{
    pthread_t  client_thread;
    pthread_t  server_thread;

    pthread_create(&server_thread, NULL, server_connect, NULL);
    pthread_create(&client_thread, NULL, client_connect, NULL);

    while(1){
        sleepms(1);
        }
}

Please let me know if I am doing it the wrong way

Regards Deb

Answer

nos picture nos · May 4, 2011

Here's atleast 3 errors:

lwip_accept() returns a new socket descriptor, you should use that to read from the client, not read from the original server socket (note also that lwip_accept will block until someone actually connects to the server).

Your port numbers might be off too if you're on a little endian machine, they're normally in network byte order, you should do Serv_Addr.sin_port = htons(9999); and same for the client port - htons converts the short from host endian to network endian

You're not sending any data ! Your client waits for the server to send it something. But your server doesn't send anything, it waits for the client to send something. Nothing will happen.

Check if lwip_connect fails too, and inspect errno if your environment provides it, as it might give clues to what goes wrong