arjun gaur arjun gaur - 2 months ago 19
C Question

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

EJP EJP
Answer

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