tests: allow more time for some tests
[jimtcl.git] / examples / unix.server
blobfa529922fd45e29b882e01c15ae1a72676fe36cc
1 # Example of a unix domain stream server which sends a response
3 file delete unix.tmp
4 set s [socket unix.server unix.tmp]
6 puts "Listening on [$s sockname]"
8 $s readable {
9         # Clean up children
10         wait -nohang 0
11         set sock [$s accept]
13         # Make this server forking so we can accept multiple
14         # simultaneous connections
15         if {[os.fork] == 0} {
16                 # We don't want the unix domain path to be deleted here
17                 $s close -nodelete
19                 $sock buffering line
21                 # Get the requests, one line at a time an evaluate
22                 while {[$sock gets buf] >= 0} {
23                         set buf [string trim $buf]
24                         if {$buf in {? help}} {
25                                 set result "Enter any Tcl command to run in the server"
26                         } else {
27                                 try {
28                                         set result [eval $buf]
29                                         set result [string map [list \\ \\\\ \n \\n \r \\r] $result]
30                                 } on error {msg} {
31                                         set result "Error: $buf => $msg"
32                                 }
33                         }
35                         # Send the result back to where it came from
36                         $sock puts $result
37                 }
38         }
40         $sock close
43 # Handle signals so the socket is removed on exit
44 signal handle SIGINT SIGTERM
46 catch -signal {
47         vwait done