How to solve the crash caused by iOS sigpipe

Source: Internet
Author: User
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

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.