InterviewSolution
| 1. |
How Can I Put A Timeout On Connect()? |
|
Answer» First, create the SOCKET and put it into non-blocking mode, then call connect(). There are three possibilities:
If the connection succeeds: If the connection fails: The socket will select as readable and writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the RESULT PARAMETER of getsockopt(), but others (incorrectly) cause the getsockopt call itself to fail with the stored value as the error. First, create the socket and put it into non-blocking mode, then call connect(). There are three possibilities: If the connection succeeds: If the connection fails: The socket will select as readable and writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the result parameter of getsockopt(), but others (incorrectly) cause the getsockopt call itself to fail with the stored value as the error. |
|