Hi,

I have a problem with sockets: I have a list of services at some hosts and ports, then I have to connect to each of them until I get one that actually reply.
Normally, the first one should work, but there can be some network failure, configuration error etc.
When this happens, call to connect() hangs up for very long time, so I set an alarm to reduce this latency.
My handler() should be called after 1 second and it actually does, but then connect() doesn't fail with errno == EINTR as expected.
Instead, it keeps hanging up for a very long time, and this messes up a lot with the rest of the system.
I read that it can be done instead with a non-blocking socket and a select(), or sort of, but the code will be quite more complicated for no apparent reason, as alarm() should theorically work fine.
Please, can you tell me where I'm wrong in that?
This is a portion of my code that should illustrate my situation:

static void handler(int)
{
}

// ...

signal(SIGALRM, handler);

alarm(1); // FIXME

if(connect(fd, (const sockaddr *)&address, sizeof(address)) == -1)
{
const int error= errno;

alarm(0);

printf("%s: %s\n", ERR_MSG, strerror(error));

close(fd);

return error;
}

alarm(0);

// ...

Thanks in advance, take care.