我正在学习TCL并试图修改我书中的echo服务器以显示当前连接的客户端的数量,然后显示所有客户端的请求数。
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
}
}
我通过添加以下内容修改了上述内容:
if {[string compare $line "countconnections"] ==0} {
puts $sock
如何显示已连接客户端的数量,然后显示收到的请求数量?
编辑:
bad option "keys": must be anymore, donesearch, exists, get, names, nextelement, set, size, startsearch, statistics, or unset
while executing
"array keys echo"
答案 0 :(得分:0)
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
}
}
}