Re: Timeout for connect()

From: David Schwartz (davids_at_webmaster.com)
Date: 03/01/04


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



Relevant Pages

  • Re: Java Executor framework and blocking IO.
    ... 'cancelled' flag. ... I may be in a blocking operation such as a socket read that may never ... Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ... how I can close the socket when I invoke cancel() on the Future. ...
    (comp.lang.java.help)
  • Re: Question regarding completion routine on overlapped WSASend
    ... Due to my experience you can set Competion routine pointer to NULL, ... socket sent all data you specifed in WSASend ... I realize if WSASend returns 0 ...
    (microsoft.public.win32.programmer.networks)
  • Re: IOCP
    ... At the moment, when a socket closes properly, I have the completion routine ... socket - if the connection is lost you should not get WSAENOTSOCK. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Cancel Reading Over A Socket
    ... } catch (IOException e) { ... I want the user to cancel the run at any time in the GUI via a cancel ... If the stream or socket is closed an IOException is thrown. ... For a normal stop I would close the stream after the loop. ...
    (comp.lang.java.programmer)
  • Re: Timeout for connect()
    ... >> solution would be to close the socket once you know that the connect ... >> to put a cap on the timeout of the connect. ... >> need to leave the connectto see if it is supposed to cancel. ... If the sender knows how long a timeout would be, ...
    (comp.unix.programmer)