connect() with timeout
Im trying to put a timeout on a connect call, and it works fine if I try to contact www.google.com on say port 81, but for some reason if I try to connect to an unopen port on 127.0.0.1 or my router's IP, it succesds all the time.
The method I used is : first I set the socket to non-blocking, tried connecting with connect(), not bothering to check the return value because its always -1, then using select() to check if the socket could be written to. heres my code: Code:
bool TcpClient::Connect( const char* host, |
hmm.. i think Ive found a solution. if I just call send(sockfd,0,0,0) and I get a return value of 0, then its connected.
|
A method I have used before is to create a signal handler function for SIGALRM, and before calling connect call alarm(timeout); and after the connect call alarm(0);
When the alarm goes off and your signal handler returns, connect will exit, and you just turn off the alarm with the alarm(0). |
Hi -
There are several ways for a sockets client to "connect() with timeout": Quote:
Quote:
Quote:
PS: Here are some links: http://publib.boulder.ibm.com/infoce...2/gtpc2m2u.htm http://linuxreviews.org/man/socket/ |
All times are GMT -5. The time now is 06:26 AM. |