timeout on connect(2) call?

Armin Gruner gruner at Informatik.TU-Muenchen.DE
Fri Feb 8 00:08:03 AEST 1991


In article <1991Feb06.181726.2735 at decuac.dec.com>, mjr at decuac.dec.com (Marcus J. Ranum) writes:
|> 
|> 	Someone a few days ago was asking if there's a way to set a timeout
|> on a connect(2) call - similarly to the timeouts you can set in a select(2).
|> Did anyone have an answer to this? Turns out I need to do something similar.
|> TFM says if you try to connect on a nonblocking socket, you'll get an
|> EWOULDBLOCK if it would block - but I'd like to be able to specify the time
|> a little more precisely. Is there an elegant way to do this?
|> 
|> mjr.

Hello..
You may use a nonblocking socket, so you will get rc=-1 and errno=EINPROGRESS
if the connection cannot be made immediately. Then, use select(2) to see when
the connection has been established. If this doesn't work, you may try the
following:

dummy(s)
{
/* 
** Don't ignore the interrupt, but just do nothing, maybe you will have to 
** set the handler again
*/
}

/*
** If your system has restarting system calls, you may need:
** siginterrupt(SIGALRM, 1);
*/

signal(SIGALRM, dummy);

alarm(TIMEOUT);
rc = connect(...);
alarm(0);

The connect() will return rc=-1 and errno=EINTR.

Greetings,
	Armin.



More information about the Comp.unix.programmer mailing list