Hi,
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 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.