Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:15:58+00:00 2026-06-05T00:15:58+00:00

I have implemented a working solutions, please see my comment below Hello, and thank

  • 0

I have implemented a working solutions, please see my comment below

Hello, and thank you for taking the time to read this post!

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.

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).

In the case event for NSStreamEventHasBytesAvailable, I read data from the inputStream and log it as so:

   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 > 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;

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.

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 deviceIsConnected is set to FALSE and I display an alert informing the user that their connection to the device has dropped.

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.

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 physically 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”.

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.

Could CocoaASyncSocket be the answer to all my questions / frustrations?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:16:00+00:00Added an answer on June 5, 2026 at 12:16 am

    My previously explained theory (see comments) has indeed worked. I am now able to successfully monitor the status of the device connectivity with the following logic (please understand that the following chunks of code exist throughout the application). I am able to determine the exact moment the device becomes unavailable; be it due to lack of WiFi connectivity, or the device losing power.

    // Define two socket objects, a "Main" socket and "Observer" socket
    
    asyncSocketMain = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    dispatch_queue_t secondaryQueue = dispatch_get_current_queue();
    asyncSocketObserver = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:secondaryQueue];
    
    // On application launch, attempt to open asyncSocketMain
    [asyncSocketMain connectToHost:host onPort:port withTimeout: 2.0 error:&error]
    
    // Determine which socket object is connecting in the didConnectToHost method
    - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
    {
    NSString *currentSocketId = (NSString *)sock;
    
    // Determine Which Socket Object is Connecting...
    if(currentSocketId == (NSString *)asyncSocketMain)
    {
        NSLog(@"Main Socket has Connected!");
        connectionIsOpening = false;  // Allow for Future Reconnect Attempts
        deviceIsConnected   = true;   // Allow Connection Monitoring
    
        // If the Main Socket has been attempting to reconnect, stop doing that!
        if(reconnectTimer)
        {
            [ reconnectTimer invalidate ]; // Stop the reconnectTimer
            reconnectTimer = nil;          // And also set its value to nil
        }
        [ self setupMonitorTimer ];   // Begin Monitoring the Connection
    }else{
        if(currentSocketId == (NSString *)asyncSocketObserver)
        {
            NSLog(@"Observer Socket attempting connection! Socket: %@", sock);
        }else{
            NSLog(@"ALERT ALERT -- UNKNOWN SOCKET CONNECTING!!!");
        }
    }
    } // close void
    

    Now, when the observer socket attempts to connect, an error will be thrown. This is how I am able to determine current connectivity. The observer will always throw error code 7 if the asyncSocketMain socket is already connected. If asyncSocketObserver times out when attempting to connect, that means the device is either powered off, out of range, or otherwise unavailable (e.g. the users phone is not connected to the correct WiFi network). In this case, all “monitoring” should be halted, and a timer is started for asyncSocketMain to attempt reconnects.

    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
    {
    
    
    NSString *connectingSocket = (NSString *)sock;
    
    // Figure out hte Error Code Info  
    NSError * error;
    error = err;
    NSInteger errorNum = error.code;
    
    
    if(connectingSocket == (NSString *)asyncSocketMain)
    {
        // This may occur if the app is opened when the device is out of range
        NSLog(@"The MAIN SOCKET Encountered an Error while Connecting! [ CODE: %d ]", errorNum);
        [ asyncSocketMain disconnect ]; // Disconnect the Main Socket to allow for reconnects
        connectionIsOpening = false;    // Allow Main Connection Attempts
        deviceIsConnected = false;      // Device is NOT CONNECTED -- Do NOT RUN MONITORING
        if(!reconnectTimer)
        {
            NSLog(@"Starting the reconnectTimer");
            [ self setupReconnectTimer ];   // Start attempting to reconnect
        }else{
            NSLog(@"Reconnect Timer is Already Running!");
        }
    }
    else
    if(connectingSocket == (NSString *)asyncSocketObserver)
    {
        switch (errorNum)
        {
            case 1:
                // Not much to do here...
                NSLog(@"OBSERVER ERROR - There is already a socket attempting to connect!");
                break;
    
            case 2:
                // Not much to do here...
                NSLog(@"OBSERVER ERROR - Event 2");
                break;
    
            case 3:
                // Time Out -- The device is out of range. Halt observer connection attempts, disconnect the main
                // socket object, then proceed to attempt to reconnect with the main socket.
                NSLog(@"OBSERVER ERROR - Connected Timed out -- Device not available!!!!");
    
                // The Observer Socket Timed out -- It's time to start reconnecting
                connectionIsOpening = false; // Allow Main Connection Attempts
                deviceIsConnected   = false; // Device is NOT CONNECTED - DO NOT RUN MONITORING and ALLOW CONNECTION ATTEMPTS
                if(monitorTimer)
                {
                    // Stop trying to reconnect with the observer socket, thus allowing the Main socket to connect
                    NSLog(@"Stopping the Monitoring Method...");
                    [monitorTimer invalidate];
                    monitorTimer = nil;
                }else{
                    NSLog(@"Connection Monitoring already halted!");
                }
    
                // If the reconnectTimer is not running (it shouldnt be, otherwise something is wrong) then go ahead and run it
                // This will attempt to reconnect asyncSocketMain
                if(!reconnectTimer)
                {
                    NSLog(@"Starting the reconnectTimer");
                    [ asyncSocketMain disconnect ]; // Deallocate the main socket to allow for reconnects
                    [ self setupReconnectTimer ];
                }else{
                    NSLog(@"Reconnection Attempts are already happening! [ reconnectTimer: %@ ]",reconnectTimer);
                }
    
                break;
    
            case 7:
                NSLog(@"OBSERVER ERROR - The Main Socket is Already Connected!");
                break;
        }
    }
    else{
        NSLog(@"An Unknown Socket Connection Encountered and Error...");
    }
    } // end void
    

    For reference, I write all data out on asyncSocketMain. The asyncSocketObserver object is always attempting to connect on a timer, whenever deviceIsConnected = TRUE.

    This may not be the most graceful way of monitoring the connection, but it does indeed work. As soon as I disconnect power from my device, asyncSocketObserver times out, which then (as per code) halts all “connection monitoring” and beings a “reconnect” timer, which is then invalided as soon as connection is established.

    Thank you again @rokjarc for your helpful knowledge and input, I hope the semi-pseudo code I have provided here (which does indeed function as intended!) aids some other developer, as this is something that I have struggled with a for at least a week!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have implemented a list view its working fine but when i click on
I have implemented C2DM (Android Push Notification) to my app and all is working
i have implemented code like here but it's not working for iOS 5.0, it
I have implemented Solr search in one of my .net application. Everything working fine
I'm working on a WPF project and have implemented a very simple way to
I am working on a web site with login registration functionality.I have implemented the
I have a horizontal ScrollView implemented and working. however, when I Scrol horizontally he
I have a working SOAP web service implemented with CXF in Java. What would
I have a MVP application implemented. Back button is working fine. I'd like to
I have made an android app which working fine. I implemented the Login functionality

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.