diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-08-12 12:21:18 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-15 11:02:50 +1000 |
commit | 0f4cb39eb1ebaf3cc931b450b517a177beb8c05e (patch) | |
tree | 288021a44441ecc21e785641c507eff7dbe707d6 /examples/tcp.server | |
parent | 2077c587650b0ff0332ceaacece892e38e1c436a (diff) | |
download | jimtcl-0f4cb39eb1ebaf3cc931b450b517a177beb8c05e.zip jimtcl-0f4cb39eb1ebaf3cc931b450b517a177beb8c05e.tar.gz jimtcl-0f4cb39eb1ebaf3cc931b450b517a177beb8c05e.tar.bz2 |
Make udp sockets useful
Separate socket types: udp client and udp server
Allow client sockets to 'connect' to an address
Allow server sockets to 'bind' to an address
Add support for 'sendto' and 'recvfrom'
Add socket client and server examples
Document new udp sockets
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples/tcp.server')
-rw-r--r-- | examples/tcp.server | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/tcp.server b/examples/tcp.server new file mode 100644 index 0000000..ef71fe5 --- /dev/null +++ b/examples/tcp.server @@ -0,0 +1,36 @@ +# Example of a udp server which sends a response + +# Listen on port 20000. No host specified means 0.0.0.0 +set s [socket stream.server 20000] + +$s readable { + set sock [$s accept] + + # Make this server forking so we can accept multiple + # simultaneous connections + if {[os.fork] == 0} { + $s close + + # Get the request (max 80 chars) - need the source address + while {[$sock gets buf] >= 0} { + set buf [string trim $buf] + puts -nonewline "read '$buf'" + + try { + set result "$buf = [expr $buf]" + } on error {msg} { + set result "Error: $buf => $msg" + } + + puts ", sending '$result'" + + # Send the result back to where it came from + $sock puts $result + $sock flush + } + } + + $sock close +} + +vwait done |