Socket Connect Timeout

How do I set the socket connect timeout? Normally there is a setsockopt() called SO_CONNECT_TIME, but that seems to be missing. A three minute wait (default) is a bit silly.

Thanks,
-Erik

Two years and no one has answered this?

Anyway, anyone got a good solution? Two minutes is a long time to wait to find out the external host for a connect isn’t out there.

You can use the setsockopt to set the various options. Additionally when opening the port you can pass it the O_NONBLOCK option. I.E. open(“/com/0/”, O_RDWR | O_NONBLOCK). This will allow the connect to return immediately and then you can check back with a select to see if the connection completed.

My current code is something like this:

sock = socket (AF_INET, SOCK_STREAM, 0);
setsockopt(sock, SOL_SOCKET, SO_BIO, (char *)&not_used, sizeof(not_used));
server.sin_family = AF_INET;
server.sin_port = myParams.Host_Port;
server.sin_addr.s_addr = htonl(DNSgethostbyname((char *)&myParams.Host_Addr));
if (connect (sock, &server, sizeof(server)) >= 0)
{
do something…
}

Are you suggesting something more like this?

timeval timeout = {5,0}; // 5 second timeout
sock = socket (AF_INET, SOCK_STREAM, 0);
setsockopt(sock, SOL_SOCKET, SO_NBIO, (char *)&not_used, sizeof(not_used));
server.sin_family = AF_INET;
server.sin_port = myParams.Host_Port;
server.sin_addr.s_addr = htonl(DNSgethostbyname((char *)&myParams.Host_Addr));

err = connect (sock, &server, sizeof(server));
while (err == some_value_that_says_still_working)
err = select(set_size, &read_set, &write_set, &exception_set, &timeout);
if (err == some_value_that_says_connected)
{
setsockopt(sock, SOL_SOCKET, SO_BIO, (char *)&not_used, sizeof(not_used));
do something…
}

So, what would I put for:

some_value_that_says_still_working
set_size
read_set
write_set
exception_set
some_value_that_says_connected

Sorry, I’m not at all familiar with socket level programming, and none of the example apps do anything like this.

Thanks,
Jeff.