├── tnm-client.tcl ├── tcludp-client.tcl ├── tnm-server.tcl ├── tcludp-server.tcl └── README.md /tnm-client.tcl: -------------------------------------------------------------------------------- 1 | # 2 | # client example using Tnm 3 | # 4 | # type lines and they are sent to a port on localhost 5 | # 6 | 7 | package require Tnm 8 | 9 | proc main {{argv ""}} { 10 | set udp [Tnm::udp create] 11 | 12 | puts "type stuff" 13 | 14 | while {[gets stdin line] >= 0} { 15 | puts "writing '$line'" 16 | $udp send localhost 7652 "$line\n" 17 | } 18 | } 19 | 20 | if !$tcl_interactive { 21 | main $argv 22 | } 23 | -------------------------------------------------------------------------------- /tcludp-client.tcl: -------------------------------------------------------------------------------- 1 | # 2 | # client example using tcludp 3 | # 4 | # 5 | 6 | package require udp 7 | 8 | proc main {{argv ""}} { 9 | set sock [udp_open] 10 | fconfigure $sock -buffering line -translation binary -remote [list localhost 7652] 11 | puts [fconfigure $sock] 12 | 13 | puts "type stuff" 14 | 15 | while {[gets stdin line] >= 0} { 16 | puts "writing '$line'" 17 | puts $sock $line 18 | #puts -nonewline $sock $line 19 | #flush $sock 20 | } 21 | } 22 | 23 | if !$tcl_interactive { 24 | main $argv 25 | } 26 | -------------------------------------------------------------------------------- /tnm-server.tcl: -------------------------------------------------------------------------------- 1 | # 2 | # server example using Tnm 3 | # 4 | # 5 | 6 | package require Tnm 7 | 8 | proc udp_callback {} { 9 | lassign [$::udpHandle receive] clientaddr clientport message 10 | set message [string trimright $message "\r\n"] 11 | puts "received [string length $message] chars from peer $clientaddr $clientport: '$message'" 12 | } 13 | 14 | 15 | proc main {{argv ""}} { 16 | set ::udpHandle [Tnm::udp create -read udp_callback -myport 7652] 17 | 18 | puts "listening on udp port: 7652" 19 | 20 | vwait die 21 | } 22 | 23 | if !$tcl_interactive { 24 | main $argv 25 | } 26 | -------------------------------------------------------------------------------- /tcludp-server.tcl: -------------------------------------------------------------------------------- 1 | # 2 | # server example using tcludp 3 | # 4 | # 5 | 6 | package require udp 7 | 8 | proc udp_callback {sock} { 9 | if {[gets $sock packet] < 0} { 10 | puts "nothing there" 11 | return 12 | } 13 | set peer [fconfigure $sock -peer] 14 | puts "received [string length $packet] chars from peer $peer: '$packet'" 15 | } 16 | 17 | 18 | proc main {{argv ""}} { 19 | #set sock [udp_open -myport 7652] 20 | set sock [udp_open 7652] 21 | #fconfigure $sock -buffering line -translation binary -blocking 0 22 | fconfigure $sock -buffering none -translation binary -blocking 0 23 | 24 | fileevent $sock readable [list udp_callback $sock] 25 | 26 | puts "listening on udp port: [fconfigure $sock -myport]" 27 | 28 | vwait die 29 | } 30 | 31 | if !$tcl_interactive { 32 | main $argv 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tcl UDP examples 2 | 3 | These examples assume you have scotty installed, for the Tnm examples, and tcludp installed, for the tcludp examples. 4 | 5 | We are using scotty from https://github.com/flightaware/scotty 6 | 7 | We are using tcludp from https://github.com/mpcjanssen/tcludp 8 | 9 | * tcludp-client.tcl - run a client that reads from stdin and sends lines to localhost port 7652 using tcludp 10 | 11 | * tnm-client.tcl - run a client that reads from stdin and sends lines to localhost port 7652 using Tnm 12 | 13 | * tcludp-server.tcl - run a server using tcludp that reads from port 7652 and emits what it gets to stdout 14 | 15 | * tnm-server.tcl - run a server using Tnm for udp that reads from port 7652 and emits what it gets to stdout 16 | 17 | 18 | ### FreeBSD 19 | 20 | What we find on FreeBSD is that using either client program, the server program runs "one message behind" at all times, meaning the first message is only "received" by Tcl after the second message has been received, the second message is only received by Tcl once the third message has been sent and so on. 21 | 22 | When we change to line buffered, things work better, but we still get a callback every other message with no data (gets returns < 0). 23 | 24 | ### nc 25 | 26 | this is another way to send: 27 | 28 | ``` 29 | nc -u 127.0.0.1 7652 30 | ``` 31 | 32 | --------------------------------------------------------------------------------