Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Socket error operation couldn’t be completed Broken pipe

Tags:

ios

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?

like image 519
Stella Avatar asked Feb 20 '14 13:02

Stella


People also ask

How do you fix a broken pipe error?

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.

What causes TCP broken pipe?

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.

What does broken pipe mean?

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.


1 Answers

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:

  • Play audio: the app can continue playing and/or recording audio in the background.
  • Receive location updates: the app can continue to get callbacks as the device’s location changes.
  • Perform finite-length tasks: the generic “whatever” case, where the app can run arbitrary code for a limited amount of time.
  • Process Newsstand Kit downloads: specific to Newsstand apps, the app can download content in the background.
  • Provide Voice-over-IP (VoIP) services: the app can run any arbitrary code in the background. Of course, Apple limits its use so that your app must provide VoIP service, too.

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.

like image 108
DareDevil Avatar answered Oct 15 '22 20:10

DareDevil