Re: TCL socket connection



JudgeDread wrote:
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


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



Try a flush after every puts. HtH Wolf .



Relevant Pages

  • Re: tcludp - bug when closing 1-of-2 listening ports
    ... Listening on udp port: 1300 ... set sock ... fconfigure $srv -buffering none -translation binary ...
    (comp.lang.tcl)
  • cant send large messages over SSL socket
    ... this is a port of code that exists in c++ and java implementations where they are able to send messages with lengths of 100,000 bytes. ... is this a limitation with httplib or pyopensll? ... import SSL ... sock = socket.socket ...
    (comp.lang.python)
  • Re: tcp client socket bind problem
    ... no matter what valid information I put in the bind it always comes ... from the default ip address on the server. ... sock = socket.socket ... sock.connect((host, port)) ...
    (comp.lang.python)
  • Problem with bind/getsockname in 64-bit program.
    ... 64-bit on HPUX B.11.11. ... port is returned and obviously fails at this point). ... struct sockaddr_in getaddr; ... printf("sock is %d\n", sock); ...
    (comp.sys.hp.hpux)