Re: Socket Conection

From: David Gravereaux (davygrvy_at_pobox.com)
Date: 06/22/04


Date: Mon, 21 Jun 2004 18:24:13 -0700

amnon.friling@citicorp.com (Afri) wrote:

>Hi
>
>I am looking for a method to monitor the creation of a client socket
>connection. Meaning, a timeout mechanism which will terminate the
>socket command if the connection has not been established within a set
>up time.
>
>Thanks
>afri

First, use the -async option to connect. Second, setup an after event to
close the socket. If the connect happens before the after event, cancel
it. That's the basics. Note that if there is a DNS delay, the lookup is
blocking and can't be canceled. It would look something like this;
untested:

set host server.blablabla.com
set port 5252
set timeoutVal 15000 ;# 15 seconds

proc SockCancel {s} {
    close $s
    puts "Connection on $s canceled due to timeout"
}

proc SockWritable {s timeoutToken} {
    after cancel $timeoutToken
    if {[set err [fconfigure $s -error]] != ""} {
        puts "Got connection error for $s: $err"
        close $s
    }
    puts "Got connection for $s"
    fileevent $s writable {}
}

set s [socket -async $host $port]
fconfigure $s -blocking 0
fileevent $s writable [list SockWritable $s [after $timeoutVal [list
SockCancel $s]]]

-- 
David Gravereaux <davygrvy@pobox.com>
[species: human; planet: earth,milkyway(western spiral arm),alpha sector]


Relevant Pages

  • Re: Async Socket IO Question
    ... If you kill the process of your client app, the TCP session isn't cleanly ... Your server still thinks the connection exists. ... After a few moments, your TCP send will timeout, ... I have had more bugs in socket shutdown code ...
    (microsoft.public.dotnet.framework)
  • Re: Need some clarification and/or help with java NIO sockets
    ... >> block while the connection is being established. ... >> a limit (connection timeout) to the operation, ... >> I tried calling closeon the socket (since I call connect on the ...
    (comp.lang.java.programmer)
  • select() returns and indicates writability before a non-blocking TCP socket has actually connect
    ... selectwill return if either the connection is made (server starts up ... and begins accepting connections) or the timeout expires. ... the socket is closed and a new socket along with a new call to ... int connectRC; ...
    (comp.os.linux.networking)
  • select() indicates writability before a non-blocking TCP socket has actually connected
    ... selectwill return if either the connection is made (server starts up ... and begins accepting connections) or the timeout expires. ... the socket is closed and a new socket along with a new call to ... int connectRC; ...
    (comp.os.linux.development.apps)
  • Re: setQueryTimeout not work?
    ... operation cancel after timeout. ... I sure that timeout with .NET stop waiting for unplug cable, ... I not think that have app that prefer ... attempts, statements etc. (never have one thread per connection), ...
    (microsoft.public.sqlserver.jdbcdriver)