[RESOLVED] How can I restart a thread at midnight?
[EDITED]
Hello,
I am running Ubuntu 11.04.
I have an application that downloads data from the remote web server. This web server shuts down for maintenance at midnight for about ten minutes. So I need to disconnect from the server at 11:55pm and connect again at 12:05am. I use sockets so to disconnect I need to logout and close the socket, and to re-connect I need to open socket and logon. The part of my application that downloads data runs in its own thread.
So I need to stop the thread at 11:55pm and start at 12:05am. How can I do that in non-blocking way? I do not want to run a loop and poll for system time that will suck up CPU time.
TIA!
Last edited by vincegata; January 19th, 2012 at 02:30 PM.
The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.
In order to avoid having these reconnection attempts use too many resources, it is common to double the amount of wait time after each failed connection attempt, up to some reasonable maximum (perhaps 2 minutes).
The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.
I need this too to make my app robust - to reconnect if the server unexpectedly drops the connection. I do not know how to check if the server is no longer connected. I'll post this question in sockets sub-forum.
If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).
If you're using UDP you will need to write the keepalive logic yourself.
If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).
>> I need my app to be able to recover from connection loss anyway.
At least you won't have to change to clock to test your recover code - just pull the plug
Bookmarks