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-27 12:06 AM

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

Re: 6.2 - How can I put a timeout on connect()?


Andrew Gierth ([email protected]) has outlined the following procedure for using select() with connect(), which will allow you to put a timeout on the connect() call:

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.

Sample code that illustrates this can be found in the file .

From: warren

That is great, but how do you actually implement the timeout?

From: Vic Metcalfe

In the above example (given as a link at the bottom) you would add the timeout to the select() call in main().

From: Stan Driggs

Another common solution is to use an alarm to interrupt the connection. Here is some example code:

static int sTimeout = 0; 

static void AlarmHandler(int sig) 
  sTimeout = 1; 

   signal(SIGALRM, AlarmHandler); 
   sTimeout = 0; 

   if ( connect(sock, (struct sockaddr *) &server, sizeof(server)) ) 
      if ( sTimeout ) 
         perror("timeout connecting stream socket"); 
         perror("connecting stream socket"); 

   sTimeout = 0; 

From: Mark Papadakis

The alarm way is not really useful if you are writting a threaded application...
You should therefore rely on the select() solution.


From: Tan Nguyen

True. In a threaded application, alarm() is not helpful at all. However, how can we use select() to interrupt connect() if connect() never returns? (ie trying to connect to some non-existent host)

From: Tan D Nguyen

Ignore my last post. Another thing though, it seems that when we set the socket to non-block mode, we cannot read the stream. I tested it by adding something to the bottom of the select() example. recv() always returns -1. Am I missing something?

if (rc) 
        fprintf(stderr,"connect failed - error %d (%s)\n",rc,strerror(rc)); 
        int sent = 0; 
        int received = 0; 
        fprintf(stderr,"connect successful\n"); 
        sent = send(sock, "GET / HTTP/1.0 \r\n\r\n", 19, 0); 
        printf("Bytes sent : %d\n", sent); 
        char buf[1024]; 
        received = recv(sock, buf, 1023, 0); 
        printf("Bytes received : %d\n", received); 
        buf[1024] = '\0'; 
        if (received > 0) 
           printf("Recieved some data\n"); 

    return 0;

From: jsh

Use setsockoption system call to set the socket to timout state.

From: andrew

Linux socket timeouts can't be amended using setsockopt.
The timeouts are fixed on a per protocol basis.

The man 7 socket page reccommends using alarm ....

From: Hector Lasso

If the socket is still in non-blocking mode then your recv() will return -1 and errno will be EAGAIN unless there is data available at this time.

From: imran
Added on: 2002-03-30 16:56:20

does anybody actually have the code to implement the
timeout using select() and non-blocking sockets ?
if so..pls post it..i'm in real need of it..
thank you.

From: Rob Seace
Added on: 2002-03-31 13:26:50

The code to do this can be found in Stevens' UNPv1 sample source... (As can code to do a variety of other things...) Specifically, look at the file "unpv12e/lib/connect_nonb.c", after extracting the files from the archive...


Board footer

Powered by FluxBB