Improve comments for JimResetHashTable()
[jimtcl.git] / examples / client-server.tcl
blob01b1ed218801cac9f4233658728c18a3954a6a90
1 proc bgerror {msg} {
2 puts "bgerror: $msg"
3 #exit 0
6 proc verbose {msg} {
7 puts $msg
10 if {[os.fork] == 0} {
11 verbose "child: waiting a bit"
13 # This will be our client
15 sleep .1
17 set f [socket stream localhost:9876]
18 fconfigure $f -buffering line
20 set done 0
22 proc onread {f} {
23 if {[$f gets buf] > 0} {
24 verbose "child: read response '$buf'"
25 } else {
26 verbose "child: read got eof"
27 set ::done 1
31 proc onwrite {f} {
32 verbose "child: sending request"
33 $f puts -nonewline "GET / HTTP/1.0\r\n\r\n"
34 $f writable {}
37 $f writable [list onwrite $f]
38 $f readable [list onread $f]
40 alarm 10
41 catch -signal {
42 verbose "child: in event loop"
43 vwait done
44 verbose "child: done event loop"
46 alarm 0
47 $f close
48 exit 0
51 verbose "parent: opening socket"
52 set done 0
54 # This will be our server
55 set f [socket stream.server 0.0.0.0:9876]
57 proc server_onread {f} {
58 verbose "parent: onread (server) got connection on $f"
59 set cfd [$f accept]
60 verbose "parent: onread accepted $cfd"
62 verbose "parent: read request '[string trim [$cfd gets]]'"
64 $cfd puts "Thanks for the request"
65 $cfd close
67 verbose "parent: sent response"
69 incr ::done
72 $f readable [list server_onread $f]
74 alarm 10
75 catch -signal {
76 vwait done
78 alarm 0
79 $f close
81 sleep .5
83 return "ok"