How to handle connection reset by peer?

In this program, when I want to connect several clients to the server, for example, 10 consecutive connections, from some connection I get the connection is reset by peer or connection refused why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>  
#include <string.h>   //strlen  
#include <stdlib.h>  
#include <errno.h>  
#include <unistd.h>   //close  
#include <arpa/inet.h>    //close  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros  
     
#define TRUE   1  
#define FALSE  0  
#define PORT 8888  
     
int main(int argc , char *argv[])   
{   
    int opt = TRUE;   
    int master_socket , addrlen , new_socket , client_socket[30] ,  
          max_clients = 30 , activity, i , valread , sd;   
    int max_sd;   
    struct sockaddr_in address;   
         
    char buffer[1025];  //data buffer of 1K  
         
    //set of socket descriptors  
    fd_set readfds;   
         
    //a message  
    char *message = "ECHO Daemon v1.0 \r\n";   
     
    //initialise all client_socket[] to 0 so not checked  
    for (i = 0; i < max_clients; i++)   
    {   
        client_socket[i] = 0;   
    }   
         
    //create a master socket  
    if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)   
    {   
        perror("socket failed");   
        exit(EXIT_FAILURE);   
    }   
     
    //set master socket to allow multiple connections ,  
    //this is just a good habit, it will work without this  
    if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,  
          sizeof(opt)) < 0 )   
    {   
        perror("setsockopt");   
        exit(EXIT_FAILURE);   
    }   
     
    //type of socket created  
    address.sin_family = AF_INET;   
    address.sin_addr.s_addr = INADDR_ANY;   
    address.sin_port = htons( PORT );   
         
    //bind the socket to localhost port 8888  
    if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)   
    {   
        perror("bind failed");   
        exit(EXIT_FAILURE);   
    }   
    printf("Listener on port %d \n", PORT);   
         
    //try to specify maximum of 3 pending connections for the master socket  
    if (listen(master_socket, 3) < 0)   
    {   
        perror("listen");   
        exit(EXIT_FAILURE);   
    }   
         
    //accept the incoming connection  
    addrlen = sizeof(address);   
    puts("Waiting for connections ...");   
 
         
    return 0;   
}

Last edited on
"Connection refused" means that you tried to connect to a host-port combination on which nobody was listening, or that a firewall blocked the connection.
"Connection reset by peer" means that the other end dropped the connection. Maybe the cable was cut, or the other process died, or maybe it simply hung up.

For both errors, there's nothing really you can do at the code level. For the connection reset, the client can try to reconnect and resend the message it was trying to send, if that's allowable by your protocol. There's nothing the code can do for a connection refused. You need to look at your infrastructure and figure it out.
Last edited on
@helios if I send a multi connection with delay between it, code work fine like:
But if I send without delay and I send consecutive connections I get those error in some connection
So I thought the code could not handle multi connection and I was looking for help to manage it
Last edited on
3 is a very small number to pass to listen.

https://www.man7.org/linux/man-pages/man2/listen.2.html

@salem c, I increased it but I still see those errors.
 
if (listen(master_socket, 30) < 0)
Last edited on
if you lose connection, you need to detect it and then attempt to reconnect every so often, say every 5 seconds, or whatever seems reasonable to you. If this is not a normal network glitch, then something in your code may not be correct. Did it work for a while and then stop working? You may not be cleaning up connections and have hit the limit on the target machine, in that case (reboot target, and fix code to close them).
Last edited on
Topic archived. No new replies allowed.