I am sending some data(image bytes) from iOS app to socket server (java-desktop) in every some intervals. Its sending data properly. I am seeing a strange issue, if the iOS device screen goes off while sending the data from iOS app, and then if I screen on the device, then i get the following error and app has got disconnected with socket or sometimes it crashes the app:
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 screen turned Off, My iOS app stops sending data to socket. and then turning ON the screen back, socket connection gets disconnected / broken pipe error. How to solve it? I searched but couldn't find the solution yet. Could someone please advise what could be the reason for this issue and how to solve this?
This kind of error can easily be fixed with a command like “sudo apt install –f”. On rare occasions, you may have experienced a broken pipe error. A pipe in Linux / Unix connects two processes, one of them has read-end of the file and the other one has the write-end of the file.
Share: The broken pipe is a TCP/IP error occurring when you write to a stream where the other end (the peer) has closed the underlying connection. The first write to the closed connection causes the peer to reply with an RST packet indicating that the connection should be terminated immediately.
A pipe is a data stream, typically data being read from a file or from a network socket. A broken pipe occurs when this pipe is suddenly closed from the other end. For a flie, this could be if the file is mounted on a disc or a remote network which has become disconnected.
You have 2 options.
1. Disable idle timer: This code will prevent your iPhone from going to sleep while your app is running. I'm not sure if this prevents the device from locking, but you can prevent the screen from dimming with the UIApplication's idleTimerDisabled property.
[UIApplication sharedApplication].idleTimerDisabled = YES;
From the documentation:
Important: You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.
2. Make an app with background support: You may follow this tutorial on Background Modes in iOS.
Here’s a quick overview of the five basic background modes available in iOS:
VOIP background mode allows your app to run arbitrary code while in the background. This mode is better than the “Whatever” API because you can run the code for an indefinite amount of time. Better yet, if the app crashes or the user reboots the phone, the app is started automatically in the background. If you are interested, you may follow Tips for Developing a VoIP App provided by apple. Your app needs to provide the user with some sort of VoIP functionality or Apple will reject your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With