Results 1 to 1 of 1
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-23-2011 #1
- Join Date
- Aug 2011
Problem with alarm() and connect()
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)
alarm(1); // FIXME
if(connect(fd, (const sockaddr *)&address, sizeof(address)) == -1)
const int error= errno;
printf("%s: %s\n", ERR_MSG, strerror(error));
Thanks in advance, take care.