Linux C + + network communication--broken pipe, sudden disconnection of receiving end, sending confiscated, still sending messages, will cause the process to crash

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.