Skip to content
Tcl Source Code View Ticket
Login

View Ticket

[fconfigure $socket -error] broken

Closed / Wont Fix218945ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:08:27
OriginalBugID: 2868 Bug
Version: 8.2
SubmitDate: '1999-09-23'
LastModified: '1999-09-29'
Severity: SER
Status: Released
Submitter: techsupp
ChangedBy: hobbs
OS: Windows 95
Machine: X86
FixedDate: '1999-09-28'
FixedInVersion: 8.2.1
ClosedDate: '2000-10-25'


Name:
Don Porter

ReproducibleScript:
#!/bin/sh
    # FILE: async
    # Usage: async <host> <port>
    #  exec tclsh "$0" ${1+"$@"}
    
    proc AsyncConnect {host port} {
        puts "creating socket..."
        set s [socket -async $host $port]
        puts "socket created"
        fileevent $s writable [list VerifyConnection $s $host $port]
    }
    
    proc VerifyConnection {s host port} {
        global connected
        fileevent $s writable {}
    
        # If there's an error condition on the socket (failure to connect),
        # then [fconfigure -error] should provide an error message
        #
        set msg [fconfigure $s -error]
        if {![string match {} $msg]} {
            puts "Connection to $host:$port failed: $msg"
            set connected 0
            catch {close $s}
            return
        }
    
        # Connection reportedly successful.  At this point we normally
        # configure the socket for whatever communications we want and
        # set up fileevent handlers to manage protocols over the socket.
        #
        # Here, as a simple check, print out the peername information
        # as a check of a good connection.  If we really are connected,
        # we print information about the far end of the connection.  If
        # not, print an error message indicating that Tcl's fileevent,
        # fconfigure, and socket are not working as advertised.
        #
        if {[catch {fconfigure $s -peername} peerInfo]} {
    puts "Tcl is broken: $peerInfo"
        } else {
    puts "Connected: $peerInfo"
        }
        set connected 1
        close $s
    }
    
    proc tick {} {
        # Demonstrate event loop is active, and provide eventual timeout
        global tick connected
        incr tick
        puts "tick $tick"
        if {$tick >= 6000} {set connected 0; return}
        after 10 tick
    }
    
    set connected 0
    set tick 0
    tick
    eval AsyncConnect $argv
    vwait connected
    # END FILE: async

ObservedBehavior:
C:> tclsh82 async www.w3.org 90
    tick 1
    creating socket...
    socket created
    tick 2
    ...
    tick 895
    Tcl is broken: can't get peername: socket is not connected

DesiredBehavior:
If the socket is not connected, I expect [fconfigure $socket -error]
    to return a non-empty error message, so that failure to connect can
    be detected by the callback routine after a [socket -async $host $port].
    
    Currently it appears the only way to check the socket for a successful
    connection is to catch [fconfigure $socket -peername].  That is an
    expensive operation which may block for awhile.  If I didn't mind
    blocking, I wouldn't be using [socket -async] in the first place.



I am moving this to either fixed (or closed) for 8.2.1, because it
gives me:

(tkcon) 51 % AsyncConnect pop 90
creating socket...
socket created
## some waiting occurs here
Connection to pop:90 failed: connection refused

Which seems to be what the user wanted.  This is on NT/SP4. 
-- 09/28/1999 hobbs
Further notes from the user indicate that this works as expected
for 98 and NT, but not 95.  This could either be a Tcl or MS
problem, but not high priority for us to examine further. 
-- 09/29/1999 hobbs
welch Closed ticket [218945ffff]. [History diff]2000-10-26 08:32:14
dgp Ticket [218945ffff]: 4 changes [History diff]2001-04-19 03:31:29
Log in (anonymous is fine) to add a comment.