Domanda

I am learning TCL and trying to modify the echo server within my book to display the number of currently connected clients and also then display the number of request from all clients.

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]
    fconfigure $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)
} else {
    if {[string compare $line "countconnections"] ==0} {
        puts $sock

}
puts $sock $line
}
}

I modified the above by adding :

if {[string compare $line "countconnections"] ==0} {
        puts $sock

How can I display the number of connected clients and then the amount of requests received?

Edit:

bad option "keys": must be anymore, donesearch, exists, get, names, nextelement, set, size, startsearch, statistics, or unset
    while executing
"array keys echo"
È stato utile?

Soluzione

proc Echo {sock} {
    global echo num_req

    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)
        return
    }

    incr num_req

    switch -exact $line { 
        "quit" {
            # Prevent new connections. 
            # Existing connections stay open. 
            close $echo(main)
        }
        "countconnections" {
            puts $sock [llength [array names echo]]  ;# number of connections
            puts $sock $num_req                      ;# number of requests
        }
        default {
            puts $sock $line
        } 
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top