IOS: operation with socket error could not be completed Broken pipe

I send some data (image bytes) from an iOS application to a socket server (java-desktop) at each time interval. Its sending data properly. I see a strange problem if the screen of the iOS device turns off when sending data from the iOS application, and then if I find the screen on the device, I get the following error and the application disconnected from the socket, or sometimes it causes the application to crash

Error writing to stream <__NSCFOutputStream: 0x1f5dd120>: Error Domain=NSPOSIXErrorDomain Code=32 "The operation couldn't be completed. Broken pipe" Stream space : 0 NSStreamEventErrorOccurred - Can not connect to the host 

When the device’s screen is turned off, the My iOS app stops sending data to the socket. and then turning the screen back on, the socket connection will be disconnected / broken pipe error. How to solve it? I searched, but could not find a solution. Can someone please advise what could be causing this problem and how to solve this problem?

+6
source share
2 answers

You have 2 options.

1. Turn off the idle timer: This code will not allow your iPhone to sleep while your application is running. I'm not sure if this will prevent the device from locking, but you can prevent the screen from using the UIpplication idleTimerDisabled function.

 [UIApplication sharedApplication].idleTimerDisabled = YES; 

From the documentation:

Important: you should set this property only if necessary, and you must be sure that reset is NOT when the need no longer exists. Most applications should let the system turn off the screen when the idle timer expires. This includes sound applications. With proper use of the audio session services, playback and recording continue uninterrupted when the screen turns off. The only applications that should turn off the idle timer are matching applications, games, or similar programs with sporadic user interactions.

2. Make a background-enabled app: You can follow this guide to Backgrounds in iOS .

Here's a quick overview of the five main background modes available in iOS:

  • Play audio: the application can continue to play and / or record sound in the background.
  • Receive location updates: The app can continue to receive callbacks when changing device locations.
  • Perform tasks of finite length: a common β€œany” case where an application can run arbitrary code for a limited period of time.
  • Downloadable Newsstand Kit downloads: specific to Newsstand applications, the application can download content in the background.
  • Providing Voice-over-IP (VoIP) services: the application can run any arbitrary code in the background. Of course, Apple restricts its use, so your application must also provide VoIP services.

Background VOIP allows your application to run arbitrary code in the background. This mode is better than the Independent API because you can run the code indefinitely. Even better, if the application crashes or the user restarts the phone, the application starts automatically in the background. If you're interested, you can follow Apple's VoIP application development tips . Your application must provide the user with some kind of VoIP feature, or Apple will reject your application.

+7
source

All Articles