sys_vgui("package require Img\n"); sys_vgui("proc Echo_Server%d {port} {\n", whoami); sys_vgui(" set s [socket -server EchoAccept%d $port]\n", whoami); sys_vgui(" puts \"server%d socket created on port $port\"\n", whoami); sys_vgui("}\n"); sys_vgui("proc EchoAccept%d {sock addr port} {\n", whoami); sys_vgui(" global echo\n"); sys_vgui(" # Record the client's information\n"); sys_vgui(" puts \"Accept $sock from $addr port $port\"\n"); sys_vgui(" set echo(addr,$sock) [list $addr $port]\n"); sys_vgui(" # Ensure that each \"puts\" by the server\n"); sys_vgui(" # results in a network transmission\n"); sys_vgui(" fconfigure $sock -buffering line\n"); sys_vgui(" # Set up a callback for when the client sends data\n"); sys_vgui(" fileevent $sock readable [list Echo%d $sock]\n", whoami); sys_vgui("}\n"); sys_vgui("# Echo --\n"); sys_vgui("# This procedure is called when the server\n"); sys_vgui("# can read data from the client\n"); sys_vgui("# Arguments:\n"); sys_vgui("# sock The socket connection to the client\n"); sys_vgui("proc Echo%d {sock} {\n", whoami); sys_vgui(" global echo\n"); sys_vgui(" # Check end of file or abnormal connection drop,\n"); sys_vgui(" # then echo data back to the client.\n"); sys_vgui(" if {[eof $sock] || [catch {gets $sock line}]} {\n"); sys_vgui(" close $sock\n"); sys_vgui(" puts \"Close $echo(addr,$sock)\"\n"); sys_vgui(" unset echo(addr,$sock)\n"); sys_vgui(" } else {\n"); sys_vgui(" #puts $sock $line\n"); sys_vgui(" imgPREVIEW%d put $line\n", whoami); sys_vgui(" }\n"); sys_vgui("}\n");