arjun gaur
arjun gaur

Reputation: 548

Getting connection reset by peer during TCP socket connection?

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

char buffer[256];

portno=5001;    
   
   /* Create a socket point */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   
   if (sockfd < 0) {
      perror("ERROR opening socket");
      exit(1);
   }
    
   
   bzero((char *) &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*)&serv_addr, sizeof(serv_addr)) < 0) {
      perror("ERROR connecting");
      exit(1);
   }
   
   /* Now ask for a message from the user, this message
      * will be read by server
   */
while(1)
{   
   printf("Please enter the message: ");
   bzero(buffer,256);
   fgets(buffer,255,stdin);
   /* Send message to the server */
   n = write(sockfd, buffer, strlen(buffer));
   
   if (n < 0) {
      perror("ERROR writing to socket");
      exit(1);
   }
   
   /* Now read server response */
   bzero(buffer,256);
   n = read(sockfd, buffer, 255);
    if(n==0)
    {
      perror("nothing to read");
    }
   
   if (n < 0) {
      perror("ERROR reading from socket");
      exit(1);
   }
    
   printf("%s\n",buffer);
}

--server.c

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 < 0) {
      perror("ERROR opening socket");
      exit(1);
   }
   
   /* Initialize socket structure */
   bzero((char *) &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 *) &serv_addr, sizeof(serv_addr)) < 0) {
      perror("ERROR on binding");
      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 < 0) {
         perror("ERROR on accept");
         exit(1);
      }
      
      /* Create child process */
      pid = fork();
        
      if (pid < 0) {
         perror("ERROR on fork");
         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("nothing to read");
    }
   
   if (n < 0) {
      perror("ERROR reading from socket");
      exit(1);
   }
   
   printf("Here is the message: %s\n",buffer);
   n = write(sock,"I got your message",18);
   
   if (n <= 0) {
      perror("ERROR writing to socket");
      exit(1);
   }
    
}

when i run the both,O/P is following:

Please enter the message: arjun
I got your message
Please enter the message: gaur
ERROR reading from socket: Connection reset by peer

Please help.how to resolve this

Upvotes: 1

Views: 13767

Answers (3)

Your client does this:

  • Connect to the server
  • Send some data to the server
  • Receive some data from the server
  • Send some data to the server
  • Receive some data from the server
  • ...

but your server does this (possibly with multiple connections at once):

  • Wait for a connection
  • Receive some stuff from the client
  • Send some stuff to the client
  • Close the connection (by exiting the only process that has the connection open)

So the server closes the connection when the client is about to send some more data, and the client gets an error because the server closed the connection.

You need to either make the server keep receiving and sending data in a loop (so it can happen several times on the same connection), or make the client make a new connection each time it wants to send something.

Upvotes: -1

user207421
user207421

Reputation: 311008

The forked child process is exiting without closing newsockfd. On some platforms that causes a connection reset instead of an orderly close.

You also need to check the result of recv() for zero and handle it correctly, i.e. not as an error, and you also need to use a positive return value correctly, e.g. instead of

printf("%s\n",buffer);

it should be

printf("%.*s\n",n,buffer);

Upvotes: 2

alk
alk

Reputation: 70971

What happens is:

  1. Server starts listening.
  2. Client connects to listening server.
  3. Server forks of process serving client.
  4. Client sends data.
  5. Serving process as of 3. receives date from client and prints it.
  6. Serving process as of 3. sends response to client.
  7. Serving process as of 3. exists/ends and with this its accepted socket (the peer to the client) gets closed.
  8. Client receives response and prints it.
  9. Client tries to send more data to the serving process as of 3..
  10. Client tries to read more data from the serving process as of 3..
  11. Step 10. fails because the serving process as of 3. already ended (step 7.)

Upvotes: 1

Related Questions