Version 23 of UDP

Updated 2003-12-09 23:06:26

UDP is an unreliable packet oriented network protocol (technically the User Datagram Protocol; datagrams are an older name for packets.) This is the datagram partner to the stream oriented TCP. The Tcl core has supported TCP sockets with the socket comment since version 7.something but UDP must be supplied by an extension. The following extensions provide UDP support:

The TclUDP package supports UDP sockets on Windows and un*x with fileevent handling and has recently been enhanced to support the TEA2 build system and to enable the transmission of binary data. It is now a project at sourceforge [L1 ] and the lastest releases will be available at [L2 ]

Also http://students.cs.tamu.edu/mmiller/tcl/channel.html which is the original base for tcludp.


UDP enabled packages

In tcllib - the dns package is capable of using udp if available. Also the forthcoming time and ntp packages. [PT]


30Jan2003 Mike Tuxford It was mentioned in c.l.t that there aren't many examples of tcludp usage around so I'll add this simple one that listens on port 1434 which is the MSSQL port and has been in the news a lot lately due to major exploits.

 proc udpEventHandler {} {
   global fd
   puts "event triggered..."
   puts "Data: [gets $fd(udp)]"
   puts "Peer: [udp_conf $fd(udp) -peer]"
   return
 }

 set fd(udp) [udp_open 1434]
 fileevent $fd(udp) readable udpEventHandler
 puts "Listening on udp port: [udp_conf $fd(udp) -myport]"

 vwait __forever__

PT 13-Jun-2003: This is a simple demo server that I use in testing:

 package require udp

 proc udpEventHandler {sock} {
     set pkt [read $sock]
     set peer [udp_conf $sock -peer]
     puts "$peer: [string length $pkt] {$pkt}"
     return
 }

 proc udp_listen {port} {
     set srv [udp_open $port]
     fconfigure $srv -buffering none -translation binary
     fileevent $srv readable [list ::udpEventHandler $srv]
     puts "Listening on udp port: [udp_conf $srv -myport]"
     return $srv
 }

 if {$tcl_interactive} {
     puts "call udp_listen portnum to begin"
 } else {
     eval [list udp_listen] $argv
     vwait forever
 }

And a corresponding server socket can be obtained using:

 proc udp_create {host port} {
     set s [udp_open]
     udp_conf $s $host $port
     fconfigure $s -buffering none -translation binary
     return $s
 }

then use

 set sock [udp_create $server $portnum];# eg localhost 9876
 puts -nonewline $sock "MyData - including binary \0\1\2\3"

30Jan2003 Shedi Sending the Wake On Lan (the magic packet) # Usage: WakeOnLan 10.255.255.255 000783104R83

package require udp proc WakeOnLan {broadcastAddr macAddr} {

     set net [binary format H* [join [split $macAddr -:] ""]]
     set pkt [binary format c* {0xff 0xff 0xff 0xff 0xff 0xff}]

     for {set i 0} {$i < 16} {incr i} {
        append pkt $net
     }

     # Open UDP and Send the Magic Paket.
     set udpSock [udp_open];
     udp_conf $udpSock $broadcastAddr 4580;
     fconfigure $udpSock -translation binary;
     puts $udpSock $pkt
     flush $udpSock;
     close $udpSock

}


[ Category Internet ]