http://blog.csdn.net/ring0hx/article/details/28891121
In the development of the iOS long connection game encountered a problem: During the game running the player pressed the home button or other reasons game is suspended, the socket connection will not disconnect, the server in order to save resources, after a period of time will actively shut down the connection. When the player again cut back to the game, the front end does not know that the connection has been disconnected, continue to send messages through the disconnected socket, this time the Send function will trigger the SIGPIPE exception caused the program to crash.
To solve this problem we need to detect that the server has closed the connection and reconnected when send. Normally the Send function returns-1 for sending fails, but sigpipe terminates the process before send returns on iOS, so we need to ignore Sigpipe, let send return normally-1, and then reconnect to the server.
Two methods were found: 1 using signal (Sigpipe, sig_ign) to ignore Sigpipe. The experiment on the iOS7 simulator although Xcode will still capture Sigpipe, but the program will not crash, continue to execute. But it's still going to crash on the real machine. 2) using So_nosigpipe. The experiment is no longer triggering sigpipe in multiple versions of iOS, which solves the problem perfectly.
[CPP] view plain copy int set = 1; setsockopt (SD, Sol_socket, So_nosigpipe, (void *) &set, sizeof (int));
Copyright NOTICE: This article is the main original article, without the owner's permission not to reprint