In the recent C + + server, when communication between two servers, one is Logserver, one is Gameserver, Gameserver timed to Logserver heartbeat packet (that is, logserver equivalent to the server , gameserver equivalentto the client), suddenly disconnected logserver, the equivalent of a server crash, the client does not know, still send data as usual, but the corresponding connection does not exist, Gameserver will report broken pipe error, then, The process also collapsed.
Of course, the process crashes, is to do the server is the least willing to see things, so, although Logserver disconnected, Gameserver still have to run normally, can not be collapsed, online check a bit, found a solution, recorded
Add a signal processing function, send the message is an invalid connection, it will trigger the sigpipe signal, the default is the shutdown process, so write your own signal processing function, do not let the process shut down is fine
Signal (Sigpipe, handle_test);
The corresponding signal processing function
static void Handle_test (int sig) {close (ILOGSERVERFD); cout << "Logserver has close ===>" << Endl; ILOGSERVERFD = 0;}
ILOGSERVERFD is the connection to the Logserver.
When you send a message again, if you judge ilogserverfd = 0 , you will not send it again, so you can achieve the goal
Linux C + + network communication--broken pipe, sudden disconnection of receiving end, sending confiscated, still sending messages, will cause the process to crash