nonblocking select() + socket()
I would like to use the following example to timeout a connection attempt, made by a TCP socket:
Code:
/* Code:
int checkprt(int port, char *ip){ |
You can't do it the way you're thinking.
There's a non-portable (read LINUX) way of doing something similar: The socket option TCP_SYNCNT. It's defaulted to 5, which corresponds to 180 seconds. However, you could easily make this number.. say.. 2. And your connect will time out much more quickly. |
This may be worth a read http://www.beej.us/guide/bgnet/
|
Quote:
What would the best method of altering this value be without doing it permanently? |
man setsockopt
Just set the TCP_SYNCNT socket option. This has the advantage of working the way you expect on any linux machine (but other systems probably will fail). |
Quote:
Kevin Barry |
Much more easy and standard way for this. Put the socket in non-blocking mode and call the connect after that. Connect will return immediately with EINPROGRESS. Now you call the select again for this socket, with the desired timeout. Now the select will wake up once the socket gets connected or it will timeout.
This is the standard async socket programming way and I hope this is the way you wanted to follow. -jork |
All times are GMT -5. The time now is 02:19 AM. |