How to implement timeout for socket?
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:
method1 send the request and starts the waiting thread (to check timeout)
waiting thread sleeps for n seconds
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
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开发者_StackOverflow 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.
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.
精彩评论