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

  • Home
  • SEARCH
  • 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 3221758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:54:47+00:00 2026-05-17T15:54:47+00:00

I’ve been working on a application for iPad that use sockets to communicate with

  • 0

I’ve been working on a application for iPad that use sockets to communicate with another application and I’m having a lot of problems figuring out how to implement a timeout when sending data to my server application.

I have like 3 command that I send to the server depending on what type of information I need, one of this commands always send a response, but the other 2 don’t, so I need a timeout in my app to know when the server didn’t send a response.

When I create the socket, I register a callback that is called when data arrives, this callback should be listening in the background, but I have noticed that if I send data, and pause the main thread of the application (pause it with a while or with a sleep function) the callback is never called.

Since I can’t do a waiting in the main thread, I decided to create a separate thread, in this thread what I do is to sleep the thread for an amount of time (timeout) and after that I check for a flag that is only set if the callback method get called (in other words if the server send me a response), and if this flag is not set, then I know the request to the server timeout, and I can move forward.

Now, the problem is that I have methods that send 50 request to the server, the logic goes like:

  1. method1 send the request and starts the waiting thread (to check timeout)

  2. waiting thread sleeps for n seconds

  3. a – if data arrives while the waiting thread is sleep, callback method is called, set a flag indicating that data has arrived, do some stuff and call method1, and the cycle start over

    b – if data data don’t arrive, the data arrived flag remain false

  4. waiting thread wakes up, check for the data arrived flag

    a – if the flag is true (data arrived), exit the thread

    b – if the flag is false (data didn’t arrived), call method1, and exit the thread

But working in this way is introducing many problems to my application, is not behaving properly, and sometimes it mess up the calls, and I can see while debugging that the delay thread is invoked many times in a row, when it should being called just one time in every cycle (you can think of a cycle like going from 1 to 4, see above), so my guess is that the reason for my problems is the way in which I’m implementing my timeout, because if I try with a command that always send a response, I don’t have any trouble.

Does anyone can help me with a better way to implement a waiting for a timeout?

Thanks.

  • 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-05-17T15:54:48+00:00Added an answer on May 17, 2026 at 3:54 pm

    Today I had to face this problem again, and I came by this post; this was the approach that I was intending to use to solve the problem I described above, but for some reason, the while loop never ended in my application, even when the time interval was consumed.

    So I keep reading and I noticed that one of the answers in the post mentioned looking into observers for the run loop, now, that didn’t help me much, but while reading about observers I run into timers.

    So, basically what I did was, since my run loop never ended after consuming all the time, I added a timer to the run loop, when the timer expires, it calls a methods using selector, and that method sets the value of a timeout flag and as explained in one of the answers from the post, because I’m using a selector, when I change the value of the flag variable the run loop immediately notice it, and exits the while.

    And what is great about this approach is that the application doesn’t get blocked, so the callback that is listening for data arrivals from the server can do it’s job.

    Here is the code I used:

    - (IBAction)someRandomAction:(id)sender
    {
        Byte byteData[3];    
        int len = 3;
        byteData[0] = 0;
        byteData[1] = 0;
        byteData[2] = 0;
        CFDataRef refData = CFDataCreate(kCFAllocatorDefault, byteData, len);
        timeOut = socketsLibrary.dataArribal = NO;
        [socketsLibrary sendMessage:refData withTag:0];
    
        NSDate* futureDate = [NSDate dateWithTimeIntervalSinceNow:5.0];    
        NSTimer* myTimer = [[NSTimer alloc] initWithFireDate:futureDate                        
                                                    interval:0.1                        
                                                      target:self                        
                                                    selector:@selector(wakeUpMainThreadRunloop:)                        
                                                    userInfo:nil                        
                                                    repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
    
        while (!timeOut && !socketsLibrary.dataArribal && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:futureDate]){}
    
    // do something with the data you expect to receive
    
    }
    
    - (void) wakeUpMainThreadRunloop:(id)arg
    {
        // This method is executed on main thread!
        // By having it run will
        // make sure the main thread stops running the runloop
        timeOut = YES;
    }

    In this code, “dataArribal” flag is inside the socketsLibrary, when data arrive from the remote host, and the callback is invoked, it also calls a method using selector, and inside that method I do:

    dataArribal = YES;
    

    So when data has been processed this flag will make the run loop finish.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.