title: multithread server/client implementation in C
body: <p>I have just started learning basic networking concepts.I am trying to implement a multithread server-client prog in C.but the problem is instead of running multiple windows/terminals/instances for clients,i should use fork() to create children of client.so by creating children of client multiple clients will be created.now each of these child clients will communicate with the server on a thread.</p>
  
  <p>Earlier i created a similar prog but in that for multiple client you have to open multiple windows for clients and run all of them.</p>
  
  <p>I am having trouble where to modify my code (both in server and client ones.I think server one is ok.but i am having no idea where to fork() in client program and what changes should be made).</p>
  
  <p>Actually i don't want to open multiple windows to run multiple client,thats why i am using fork() to create multiple copies of it.Is there any other way by which i can create multiple clients and connect them to my server prog via threads.</p>
  
  <p>Server :</p>
  
  <pre><code>// socket server example, handles multiple clients using threads
  
  #include&lt;stdio.h&gt;
  #include&lt;string.h&gt;    //strlen
  #include&lt;stdlib.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt; //inet_addr
  #include&lt;unistd.h&gt;    //write
  #include&lt;pthread.h&gt; //for threading , link with lpthread
  
  //the thread function
  void *connection_handler(void *);
  
  int main(int argc , char *argv[])
  {
      int socket_desc , client_sock , c , *new_sock;
      struct sockaddr_in server , client;
  
      //Create socket
      socket_desc = socket(AF_INET , SOCK_STREAM , 0);
      if (socket_desc == -1)
      {
          printf("Could not create socket");
      }
      puts("Socket created");
  
      //Prepare the sockaddr_in structure
      server.sin_family = AF_INET;
      server.sin_addr.s_addr = INADDR_ANY;
      server.sin_port = htons( 3000 );
  
      //Bind
      if( bind(socket_desc,(struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
      {
          //print the error message
          perror("bind failed. Error");
          return 1;
      }
      puts("bind done");
  
      //Listen
      listen(socket_desc , 3);
  
      //Accept and incoming connection
      puts("Waiting for incoming connections...");
      c = sizeof(struct sockaddr_in);
  
          c=sizeof(struct sockaddr_in);
         while(client_sock=accept(socket_desc,(struct sockaddr*)&amp;client,(socklen_t*)&amp;c))
         {
          puts("Connection accepted");
  
          pthread_t sniffer_thread;
          new_sock = malloc(1);
          *new_sock = client_sock;
  
          if( pthread_create( &amp;sniffer_thread , NULL ,  connection_handler , (void*) new_sock) &lt; 0)
          {
              perror("could not create thread");
              return 1;
          }
  
          puts("Handler assigned");
      }
  
      if (client_sock &lt; 0)
      {
          perror("accept failed");
          return 1;
      }
      return 0;
  }
  /*
    This will handle connection for each client
    */
  void *connection_handler(void *socket_desc)
  {
      //Get the socket descriptor
      int sock = *(int*)socket_desc;
      int n;
  
          char    sendBuff[100], client_message[2000];
  
        while((n=recv(sock,client_message,2000,0))&gt;0)
        {
  
          send(sock,client_message,n,0);
        }
        close(sock);
  
        if(n==0)
        {
          puts("Client Disconnected");
        }
        else
        {
          perror("recv failed");
        }
      return 0;
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  #define MAX_SIZE 50
  
  int main()
  {
      int sock_desc;
      struct sockaddr_in serv_addr;
      char sbuff[MAX_SIZE],rbuff[MAX_SIZE];
  
      if((sock_desc = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          printf("Failed creating socket\n");
  
      bzero((char *) &amp;serv_addr, sizeof (serv_addr));
  
      serv_addr.sin_family = AF_INET;
      serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      serv_addr.sin_port = htons(3000);
  
      if (connect(sock_desc, (struct sockaddr *) &amp;serv_addr, sizeof (serv_addr)) &lt; 0) {
          printf("Failed to connect to server\n");
          return -1;
      }
  
      printf("Connected successfully - Please enter string\n");
      while(fgets(sbuff, MAX_SIZE , stdin)!=NULL)
      {
        send(sock_desc,sbuff,strlen(sbuff),0);
  
            if(recv(sock_desc,rbuff,MAX_SIZE,0)==0)
             printf("Error");
            else
             fputs(rbuff,stdout);
  
         bzero(rbuff,MAX_SIZE);//to clean buffer--&gt;IMP otherwise previous word characters also came
      }
          close(sock_desc);
      return 0;
  
  }
  </code></pre>