title: Identifying the messages from clients in multiple client single server program implemented in C
body: <p>I am a beginner in Networking. I implemented a multiple-client single-server program using sockets in C. If I run the server and two or more instances of the client then how do I identify the client who has sent a message to server. Is this implementation correct or do I have to modify it?</p>
  
  <pre><code>    //server.c
      #include &lt;stdio.h&gt;
      #include &lt;sys/types.h&gt;        
      #include &lt;sys/socket.h&gt;
      #include &lt;strings.h&gt;
      #include &lt;arpa/inet.h&gt;
      #include&lt;netinet/in.h&gt;
      #include&lt;stdlib.h&gt;
      #include&lt;unistd.h&gt;
      #define MAX 1000
  
      void serveClient(int sock)
      {
          int r;
              char buffer[MAX];
          do
          {
              bzero(buffer, sizeof(buffer));
                  r = read(sock, buffer, sizeof(buffer));
              if(strcasecmp(buffer,"bye")==0) return;
              if(r != 0)
                  printf("\nMessage from client : %s\n",buffer);
          }while(1);
      }
  
      int main()
      {
          int sockfd, newfd, len, r, pid;
          char buff[MAX];
          struct sockaddr_in servaddr,cliaddr;
  
          //creating socket
          if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          {
              perror("socket");
              exit(1);
          }
          else
              printf("\nSocket created\n");   
  
          bzero(&amp;servaddr, sizeof(servaddr));
          servaddr.sin_family = AF_INET;
          servaddr.sin_port = htons(7790);
          servaddr.sin_addr.s_addr = INADDR_ANY;
  
          //bnding a name to socket
          if(bind(sockfd, (struct sockaddr*)&amp;servaddr, sizeof(servaddr)) &lt; 0) 
          {
              perror("bind");
              exit(1);
          }
          else
              printf("\nBind successful\n");
  
          //listening for connections
          if(listen(sockfd, 5) &lt; 0)
          {
              perror("listen");
              exit(1);
          }
          else
              printf("\nListening...\n");
  
          //accepting a connection
          do
          {
              len = sizeof(cliaddr);
              if((newfd = accept(sockfd, (struct sockaddr*)&amp;cliaddr, &amp;len)) &lt; 0) 
              {
                  perror("accept");
                  exit(1);
              }
              else
                  printf("\nNew connection accepted\n");
  
              pid = fork();
              if(pid == -1)
              {
                  perror("fork");
                  close(newfd);
                  continue;
              }
              else if(pid == 0)
              {
                  serveClient(newfd);
                  close(newfd);
                  printf("\nClient terminated\n\nWaiting for new client...\n");
              }
              else 
              {
                  close(newfd);
              }
          }while(1);
          close(sockfd);
          return 0;
      }
  
  
      //client.c
      #include &lt;stdio.h&gt;
      #include &lt;sys/types.h&gt;        
      #include &lt;sys/socket.h&gt;
      #include &lt;strings.h&gt;
      #include &lt;arpa/inet.h&gt;
      #include &lt;netinet/in.h&gt;
      #include&lt;stdlib.h&gt;
      #define MAX 1000
  
      int main(int argc, char *argv[])
      {
          int len, sockfd, n;
          char buff[MAX];
      ;   struct sockaddr_in servaddr, cliaddr;
          if(argc == 2)
          {
              //creating socket
          if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          {
              perror("socket");
              exit(1);
          }
          else
              printf("\nSocket created\n");
  
          bzero(&amp;servaddr, sizeof(servaddr));
          servaddr.sin_family = AF_INET;
          servaddr.sin_port = htons(7790);
          servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  
          //initiating connection on the socket
          if(connect(sockfd,(struct sockaddr*)&amp;servaddr,sizeof(servaddr)) &lt; 0) 
          {
              perror("connect");
              exit(1);
          }
          else
              printf("\nConnected\n");
  
          //sending message
          printf("\nType \"bye\" to disconnect\n");
          do
          {
              printf("\nMessage : ");
              scanf("%s",buff);
              n = write(sockfd, buff, sizeof(buff));
              if(strcasecmp(buff, "bye") == 0) exit(0);
          }while(1);
          close(sockfd);
      }
      else 
      {
          printf("\nSpecify an IP address\n");
          exit(1);
      }
      return 0;
  }
  </code></pre>