TCL socket connection
- From: "JudgeDread" <diabolik@(nospam)uku.co.uk>
- Date: Thu, 30 Jun 2005 17:56:49 +0000 (UTC)
Hello Gurus
I have a problem using the tcl socket command. I am
using the following code to establish a socket connection from a client to a
server. The problem is that i seem to be able to get it only to echo once. How
do i establish a socket connection that stays up and does not cause
the client to hang when I issue another echo request to the
server?
much appreciated
Server
The echo service.
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)
}
puts $sock $line
}
}
Echo_Server 2540
vwait forever
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)
}
puts $sock $line
}
}
Echo_Server 2540
vwait forever
Client
proc Echo_Client {host port} {
set s [socket $host $port]
fconfigure $s -buffering line
return $s
}
set s [Echo_Client localhost 2540]
puts $s "Hello!"
gets $s
set s [socket $host $port]
fconfigure $s -buffering line
return $s
}
set s [Echo_Client localhost 2540]
puts $s "Hello!"
gets $s
- Follow-Ups:
- Re: TCL socket connection
- From: Wolf Grossi
- Re: TCL socket connection
- Prev by Date: tclhttpd/Tcl DevKit oddity
- Next by Date: Re: Commercial/Shareware and Extensions ?
- Previous by thread: tclhttpd/Tcl DevKit oddity
- Next by thread: Re: TCL socket connection
- Index(es):
Relevant Pages
|