Re: Timeout for connect()
From: David Schwartz (davids_at_webmaster.com)
Date: 03/01/04
- Next message: Materialised: "ncurses Line Input"
- Previous message: Chris: "Re: implementing a firewall from scratch"
- In reply to: Jo: "Re: Timeout for connect()"
- Next in thread: Jo: "Re: Timeout for connect()"
- Reply: Jo: "Re: Timeout for connect()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 1 Mar 2004 14:30:26 -0800
"Jo" <JoJoTwilligo@hotmail.com> wrote in message
news:72ce7f0c.0403010952.573dc94f@posting.google.com...
> That's what I was thinking of. Is there any danger in closing a
> socket that hasn't been opened? If not, then I would think the proper
> solution would be to close the socket once you know that the connect
> is to be canceled.
There is no such thing as "a socket that hasn't been opened". The open
('socket') operation creates the socket and the close ('close') function
destroys it. If you have it, it's open.
>> Just don't forget to ignore SIG_ALARM. I think you have this wrong
>> though. The second call to 'connect' will fail with EINPROGRESS.
>
> Alright, how 'bout this then:
> signal(SIGALARM,SIG_IGN);
> do {
> alarm(1);
> } while (connect(who, &cares, question_mark) == -1 && (errno==EINTR ||
> errno==EINPROGRESS);
The problem is that this will spin and burn the CPU. Each call to
'connect' (except the first) will fail immediately with EINPROGRESS. You
could put in a short sleep if you get back EINPROGRESS, but then you may
waste some time after the connection completes. This is what 'poll' or
'select' is for.
>> Can you state your actual problem? Odds are the solution has nothing
>> whatsoever to do with connection timeouts. What's the problem you have?
>> What
>> are you trying to do?
> I'm writing a daemon, so I wanted to be able to cancel the connect
> on SIGTERM, or at least know for how long the connect() would block so
> the daemon-shutdown routine would know how long to wait.
Wait until the connect fails!
> I also want
> to put a cap on the timeout of the connect(). In either situtation, I
> need to leave the connect() to see if it is supposed to cancel. If it
> is supposed to cancel, I need to free any allocated resources. But if
> it's not supposed to cancel, I need to monitor the status of the
> connect(). Everywhere else I've read recommends using a complicated
> select() routine. Why is this routine so simple?
I don't follow you here. If you initiated the 'connect', wait until it
finishes. If you decide to shutdown after the 'connect' attempt, either
abort the 'connect' right then or wait until it finishes. I don't see where
you need to know how long 'connect' might or might not take.
DS
- Next message: Materialised: "ncurses Line Input"
- Previous message: Chris: "Re: implementing a firewall from scratch"
- In reply to: Jo: "Re: Timeout for connect()"
- Next in thread: Jo: "Re: Timeout for connect()"
- Reply: Jo: "Re: Timeout for connect()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|