UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-25 06:44 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 3.5 - How to set the timeout for the connect() system call

6
From Richard Stevens (rstevens@noao.edu):

Normally you cannot change this. Solaris does let you do this, on a per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.

The easiest way to shorten the connect time is with an alarm() around the call to connect(). A harder way is to use select(), after setting the socket nonblocking. Also notice that you can only shorten the connect time, there's normally no way to lengthen it.

From Andrew Gierth (andrew@erlenstar.demon.co.uk):

First, create the socket and put it into non-blocking mode, then call connect(). There are three possibilities:

    * connect succeeds: the connection has been successfully made (this usually only happens when connecting to the same machine)
    * connect fails: obvious
    * connect returns -1/EINPROGRESS. The connection attempt has begun, but not yet completed.

If the connection succeeds:
the socket will select() as writable (and will also select as readable if data arrives)

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.

From: Jukka Santala
This answer doesn't cover how to force the actual _timeout_. I assume a simple close() will do, but it'd be nice to have the FAQ bit answer the actual question. (And yes, this is actually a situation that came up, in a server where identd query will be waited for limited time before letting the main connection in)

From: jagadeesh

Hello,
Try this

sts = connect(mysock, &msaddr, sizeof (msaddr)); 
    if (sts) 
    { 
if (errno == ECONNREFUSED) 
{ 
printf("connection refused\n"); 
return; 
} 
else if (errno == ETIMEDOUT) 
{ 
printf("connection attempt timed out\n"); 
return; 
}

From: Tan Nguyen

Nope, that doesn't solve the problem at all. For example, we attempt to connect to a non-existent host (ie 209.blah.blah.blah), then connect() just keeps trying to look up the routing table for the host. Thus, you never get a return from connect().

From: Oren Bassik

I'm not sure about this, but if you get an alarm signal while connecting, it will return with EINTR

Offline

Board footer

Powered by FluxBB