0

My client and server are sending buffer messages to each other. Following is the client side and server side section of read/write code:

Client side:

if (connect( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0) 
{
    perror("ERROR connecting");
    return(1);
}    

printf("Recieving Buffer 1 from Server side"); 

bzero(buffer1,256);
int i = 0;

while (i < 5)
{
    n = read(sockfd,buffer1,sizeof(buffer1));
    if (n<0)
        printf("ERROR reading in socket %d  len %d", n, sizeof(buffer1));

    n = write(sockfd,buff2,sizeof(buff2));
    if(n<0)
        printf("ERROR writing in socket %d  len %d", n, sizeof(buff2));

    printf("\nSent Buffer2 (WON'T TERMINAL TYPE) from client side");

    i++;

}

Server Side :

printf("Server Sending (DO TERMINAL TYPE)\n");

while(1)
{
    while ((n = write(newsockfd,buff1,sizeof(buff1)))>0)
    {
        printf("Server Sent query %d: %hhX %hhX %hhX\n",count,buff1[count][0],buff1[count][1],                   buff1[count][2]);
        n = read(newsockfd, recbuf , sizeof(recbuf));
        printf("Server received response: %hhX %hhX %hhX\n",recbuf[0],recbuf[1],recbuf[2]);
        count++;
        count = count % 3;
     }
         if (n<0)
        {
            perror("Error writing to socket ");
            exit(1);
        }
}

Output I received : server :

./server 9998
Server Sending (DO TERMINAL TYPE)
Server Sent query 1: FF FD 1E
Server received response: FF FC 18
Server Sent query 2: FF FD 1D
Server received response: FF FC 18
Server Sent query 0: FF FD 18
Server received response: FF FC 18
Server Sent query 1: FF FD 1E
Server received response: FF FC 18
Server Sent query 2: FF FD 1D
Server received response: FF FC 18
Server Sent query 0: FF FD 18
Server received response: FF FC 18
Error writing to socket : Connection reset by peer

client side :

./client 127.0.0.1 9998
Recieving Buffer 1 from Server side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side
Sent Buffer2 (WON'T TERMINAL TYPE) from client side

I would like to close the connection after i = 5 that is after 5 buffer messages are exchanged between server-client, but the program gives error of Connection reset by peer (i guess some error from client side). How should I fix this? Thanks in advance

Jonsnow29
  • 41
  • 1
  • 8
  • 1
    When `i == 5` or `connect != 0`, you should `close`/`shutdown` the connection. Same for the server. Further read: http://stackoverflow.com/questions/1434451/what-does-connection-reset-by-peer-mean – edmz Oct 17 '14 at 15:31
  • @black I tried using a if loop where i == 6 , it closes the socket on client side. Still getting the same error – Jonsnow29 Oct 17 '14 at 15:45
  • You're closing the client connection when `i == 5`, which is what you say you want to do, it is also the reason why you're seeing the `connection reset by peer` error. So what's the actual issue that you're having? – Eugene S Oct 17 '14 at 15:48
  • @unluddite I want to remove the error of Connection reset from server side output – Jonsnow29 Oct 17 '14 at 15:52
  • @Jonsnow29 If the client closes the connection, and you read/write from/to it, you'll get that error. – edmz Oct 17 '14 at 15:56
  • @black Thanks its working now, removed the if loop out of the inner while loop. – Jonsnow29 Oct 17 '14 at 16:25
  • @Jonsnow29 May you answer your own question? Please see [this](http://stackoverflow.com/help/self-answer). – edmz Oct 17 '14 at 17:09
  • @black He must have more than 15 reputation to answer his own question. I think you should answer it in this case. – whoan Oct 18 '14 at 11:51

0 Answers0