我想知道每次客户端连接时是否可以向客户端发送ping?
我已经设置了一个echo服务器,我正在尝试修改它以向每个客户端发送ping,这可能吗?
我尝试在EchoAccept函数下添加此循环,但这会导致错误:
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
由于
答案 0 :(得分:0)
您在此处有错误:
foreach connection {
127.0.0.1
}{ping $connection}
# ..^^
因为Tcl是面向单词的,所以空白非常重要:你在foreach body之前错过了一个空格