Note that there are some explanatory texts on larger screens.

plurals
  1. POMonitor Connection Status & Handling Drops
    primarykey
    data
    text
    <p><strong>I have implemented a working solutions, please see my comment below</strong></p> <p>Hello, and thank you for taking the time to read this post! </p> <p>I'm developing an app which connects to a hardware device which broadcasts its own AdHoc WiFi network. I am able to connect to the device, send bytes, and receive bytes via CFNetwork toll-free bridging with NSStream. I am using the rather "de-facto" stream opening code and the stream delegate is reporting NSStreamEvents as it should.</p> <p>Upon initializing my connection to the device, I can see both input and output streams open (NSStreamEventOpenCompleted) and as the hardware device is constantly sending "HELLO!", there are immediately BytesAvailable on the inputStream (NSStreamEventHasBytesAvailable).</p> <p>In the case event for NSStreamEventHasBytesAvailable, I read data from the inputStream and log it as so:</p> <pre><code> case NSStreamEventHasBytesAvailable: NSLog(@"CASE LOG -- NSStreamEventHasBytesAvailable"); uint8_t buffer[256]; int len; while ([inputStream hasBytesAvailable]) { //NSLog(@"LOG -- inputStream hasBytesAvailable"); len = [inputStream read:buffer maxLength:sizeof(buffer)]; if (len &gt; 0) { NSLog(@"Length of inputStream Bytes -- %i",len); NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; // This global boolean affects the rest of the apps functionality. Buttons are not able to send bytes to the hardware device if this boolean is FALSE. deviceIsConnected = true; // If buttons to send bytes are disabled due to lack of network connection on appLaunch, then go ahead and show them, allowing the user to send bytes to the hardware device if(buttonsAreDisabled == true) { [ self setButtonVisibility:true ]; // Reset the status of the "No Connection" Alert connectionAlertShown = false; } // Log the incoming data if (nil != output) { NSLog(@"LOG -- device said: %@", output); } } } break; </code></pre> <p>As expected, I have a constant stream of "LOG -- device said: xxxx" while my device is connected. However, if I disconnect the device from its power source, I do not receive any sort of Stream Event; the logging simply stops all together.</p> <p>I have attempted to remedy this issue by starting a backgroundTimer in my viewDidLoad, which every 0.1 seconds attempts to read from the inputStream. If it is not able to read, then the boolean <code>deviceIsConnected</code> is set to <code>FALSE</code> and I display an alert informing the user that their connection to the device has dropped.</p> <p>This method has proven rather unreliable, and also a very ungraceful way of going about a seemingly simple task of detecting the closing of a socket connection. If I understand correctly, the NSStream class is essentially a "Middle man" or abstraction layer above the underlying BSD Socket Architecture. </p> <p>Disconnecting the hardware device from its power source is simulating walking out of range of the device's onboard WiFi chip. This is not a "real world" test, as if you are <em>physically</em> walking away from the device, you will not suddenly lose connection; rather, the data received by the inputStream will slowly deteriorate thus causing the "Network Alert" popup to continuously flicker as the device jumps between "connected" and "not connected". </p> <p>I would like to implement some sort of KeepAlive handler, but my lack of experience with iPhone / iOS / BSD Sockets is hindering me severely. If any of you wonderful individuals could provide a basic example of a method (likely running on a timer, I think I'm on the right path there!) that can detect a socket becoming unavailable and proceed to attempt to reestablished the connection, I would be eternally grateful. I have tirelessly searched Google and found a few promising ideas, but haven't been able to successfully implement any of them.</p> <p>Could CocoaASyncSocket be the answer to all my questions / frustrations?</p> <p>Thank you again for taking the time to read over this post. I hope I have provided a clear explanation of my problem and my desired solution. Should you have any questions, please feel free to ask and I will do my best to answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload