The answers about using select()
/poll()
are right and code should be written this way to be portable.
However, since you're on Linux, you can do this:
int synRetries = 2; // Send a total of 3 SYN packets => Timeout ~7s
setsockopt(fd, IPPROTO_TCP, TCP_SYNCNT, &synRetries, sizeof(synRetries));
See man 7 tcp
and man setsockopt
.
I used this to speed up the connect-timeout in a program I needed to patch quickly. Hacking it to timeout via select()
/poll()
was not an option.