It was possible to create a bad ref
[jimtcl.git] / examples / tcp.server
blobef71fe5c8a2d9858dc093fa79bc0e158f458e956
1 # Example of a udp server which sends a response
3 # Listen on port 20000. No host specified means 0.0.0.0
4 set s [socket stream.server 20000]
6 $s readable {
7         set sock [$s accept]
9         # Make this server forking so we can accept multiple
10         # simultaneous connections
11         if {[os.fork] == 0} {
12                 $s close
14                 # Get the request (max 80 chars) - need the source address
15                 while {[$sock gets buf] >= 0} {
16                         set buf [string trim $buf]
17                         puts -nonewline "read '$buf'"
19                         try {
20                                 set result "$buf = [expr $buf]"
21                         } on error {msg} {
22                                 set result "Error: $buf => $msg"
23                         }
25                         puts ", sending '$result'"
27                         # Send the result back to where it came from
28                         $sock puts $result
29                         $sock flush
30                 }
31         }
33         $sock close
36 vwait done