title: Getting connection reset by peer during TCP socket connection?
body: <p>i'm making a small client-server application,where server forks a child process after accept,for multiple connections while the client connects to it ,sends a message and receives a response. following is my code fragment:
  -- client.c</p>
  <p>char buffer[256];</p>
  <pre><code>portno=5001;    
     
     /* Create a socket point */
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     
     if (sockfd &lt; 0) {
        perror(&quot;ERROR opening socket&quot;);
        exit(1);
     }
      
     
     bzero((char *) &amp;serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_port = htons(portno);
     
     /* Now connect to the server */
     if (connect(sockfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {
        perror(&quot;ERROR connecting&quot;);
        exit(1);
     }
     
     /* Now ask for a message from the user, this message
        * will be read by server
     */
  while(1)
  {   
     printf(&quot;Please enter the message: &quot;);
     bzero(buffer,256);
     fgets(buffer,255,stdin);
     /* Send message to the server */
     n = write(sockfd, buffer, strlen(buffer));
     
     if (n &lt; 0) {
        perror(&quot;ERROR writing to socket&quot;);
        exit(1);
     }
     
     /* Now read server response */
     bzero(buffer,256);
     n = read(sockfd, buffer, 255);
      if(n==0)
      {
        perror(&quot;nothing to read&quot;);
      }
     
     if (n &lt; 0) {
        perror(&quot;ERROR reading from socket&quot;);
        exit(1);
     }
      
     printf(&quot;%s\n&quot;,buffer);
  }
  </code></pre>
  <p>--server.c</p>
  <pre><code>int main( int argc, char *argv[] ) {
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n, pid;
     
     /* First call to socket() function */
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     
     if (sockfd &lt; 0) {
        perror(&quot;ERROR opening socket&quot;);
        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(&quot;ERROR on binding&quot;);
        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 (1) {
        newsockfd = accept(sockfd, (struct sockaddr *)NULL,NULL);
          
        if (newsockfd &lt; 0) {
           perror(&quot;ERROR on accept&quot;);
           exit(1);
        }
        
        /* Create child process */
        pid = fork();
          
        if (pid &lt; 0) {
           perror(&quot;ERROR on fork&quot;);
           exit(1);
        }
        
  
  
   if (pid == 0) 
   {
       while(1)
       {
       /* This is the client process */
       close(sockfd);
       doprocessing(newsockfd);
     /**  exit(0); **/
       }
   }
        else {
           close(newsockfd);
        }
          
     } /* end of while */
  }
  
  
  void doprocessing (int sock) {
     int n;
     char buffer[256];
     bzero(buffer,256);
     n = read(sock,buffer,255);
         if(n==0)
      {
        perror(&quot;nothing to read&quot;);
      }
     
     if (n &lt; 0) {
        perror(&quot;ERROR reading from socket&quot;);
        exit(1);
     }
     
     printf(&quot;Here is the message: %s\n&quot;,buffer);
     n = write(sock,&quot;I got your message&quot;,18);
     
     if (n &lt;= 0) {
        perror(&quot;ERROR writing to socket&quot;);
        exit(1);
     }
      
  }
  </code></pre>
  <p>when i run the both,O/P is following:</p>
  <pre><code>Please enter the message: arjun
  I got your message
  Please enter the message: gaur
  ERROR reading from socket: Connection reset by peer
  </code></pre>
  <p>Please help.how to resolve this</p>