blob: 5dede3a4e17c8f397a8ad39b338cc344358f6214 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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");
|