Modify connect() timeout period

  1. Create the socket.
  2. Put it into non-blocking mode.
  3. Issue connect().
  4. Call select() or poll() or epoll(), specifying your desired timeout, and specifying the socket as a writefd, i.e. blocking until the timeout expires or the socket becomes writable.
  5. If the timeout expires, close the socket etc.
  6. Otherwise get the last error on the socket via getsockopt() and see if it's zero. If so, the connection has succeeded, otherwise it has failed.

Note that you cannot increase the connect timeout beyond the platform default (about a minute) by this means, but you can decrease it.

Tags:

C++

C

Sockets