Question

I was wondering if its possible to send a ping to a client each time a client connects?

I have setup an echo server and I am trying to modify this to send a ping to each client, is this possible?

I have tried adding this loop beneath the EchoAccept function but this causes an error:

 foreach connection {
        127.0.0.1
        }{ping $connection}


    proc Echo_server {port} {
            global echo
            set echo(main) {socket -server EchoAccept $port}
    }
    proc EchoAccept {sock addr port} {
            global echo
            puts "Accept $sock from $addr port $port"
            set echo(addr,$sock) [list $addr $port]
            f configure $sock -buffering line
            fileevent $sock readable [list Echo $sock]
    }
    proc Echo {sock} {
            global echo
            if {[eof $sock] || [catch {gets $sock line}]} {
                    # end of file or abnormal connection drop
                     close $sock
                     puts "Close $echo(addr,$sock)"
                     unset echo(addr,$sock)
    } else {
            if {[string compare $line "quit"] ==0} {
            # Prevent new connections. 
                    # Existing connections stay open. 
            close $echo(main)
    }
    puts $sock $line
    }
    }
vwait forever

Thanks

Was it helpful?

Solution

You have an error here:

foreach connection {
    127.0.0.1
    }{ping $connection}
# ..^^

Because Tcl is word-oriented, whitespace is very important: you're missing a space before the foreach body

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top