1752402 Members
5686 Online
108788 Solutions
New Discussion юеВ

Re: connect

 
Fedele Giuseppe
Frequent Advisor

connect

Hello all,

I have met the following problem in managing "connect" function.

To manage the case in which the remote host is not available, I have written the following code:

alarm(1);

err = connect(....);

alarm(0);

so, if the remote node is not available the connect will be interrupted from incoming SIGALRM and the err variable is set to 4.

Now, in some cases such a mechanism doesn't work and the connect terminates only when its time out expires (errno = 238 - Connection timed out)

Which could be the reason?
Do you see any weakness in this procedure?

Thanks in Advance

Giuseppe


4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: connect

Hi:

What you have shown doesn't reveal the signal handler code. For that matter, the 'connect()' normally blocks (waits) until satisfied. You could use a non-blocking connection and periodically retry until you are interrupted or achieve a successful connection.

See the manpages for 'connect(2)' for more information.

Regards!

...JRF...
Fedele Giuseppe
Frequent Advisor

Re: connect

Hi, this it the signal handling code:

sigemptyset(&signalAction.sa_mask);
signalAction.sa_flags = 0;
signalAction.sa_handler = SigExit;
....
sigaction(SIGALRM,&signalAction, NULL);

where:

void SigExit(int sig)
{
switch(sig)
{
case SIGALRM:
return;
break;
}
}
David Johns
Advisor

Re: connect

Hi Giuseppe:

I think you want select(), with the timeout set to one second.

I would avoid a signal handler in this procedure, the select function is what I'd go with.

Best,
Dave Johns
rick jones
Honored Contributor

Re: connect

To use select() (or poll(), or eventports) with connect(), one would need to put the socket into non-blocking mode, make the connect() call and then go into select() (et al) for ones timeout length, waiting for the socket to become writable.

Is this a single, or a multi-threaded program?
there is no rest for the wicked yet the virtuous have no pillows