- TclUDP - This is the normal package for standard UDP client/server programs
- Tcl-dp - An extensive TCP/IP package which also supports UDP
- Scotty - An SNMP package with its own UDP implementation
- pktsrc - Access to raw IP Datagram and thus to UDP
- ceptcl - Communications EndPoints for Tcl
- Packet - A few Tcl routines for manipulating network packets
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 useset sock [udp_create $server $portnum];# eg localhost 9876 puts -nonewline $sock "MyData - including binary \0\1\2\3"
30-Jan-2003 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.
package require udp
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__10-Dec-2003 Shedi Wake On Lan Example (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
}sbron In my experiments this works on windows, but not on linux. On linux the code has to specifically allow broadcasting using something like the following at an appropriate spot in the c code:int yes = 1; setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int));PT 19-Nov-2004: The in-development version 2.x tcludp supports setting the broadcast option on the socket. If you want to try it out, checkout the dev-2 branch from the tcludp sourceforge CVS.06-Jun-2006 There is no dev-2 branch at tcludp.sourceforge.net (anymore?), can someone provide a link where to get tcludp 2.x ?Here is a sample of a simplistic chat using broadcast UDP packets using the tcludp 2.0.0 api.
# udpmsg.tcl - Copyright (C) 2004 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Demo chat application.
#
# This uses broadcast UDP packets to send chat messages to the network.
#
# $Id: 16733,v 1.2 2006-09-14 06:01:04 jcw Exp $
package require Tk 8.4
package require udp 2
variable Port 7531
variable Network 172.16.255.255
variable server
variable client
# handle incoming packets on the udp server socket.
proc udpEventHandler {sock} {
set pkt [read $sock]
set peer [fconfigure $sock -peer]
AddMessage "$peer $pkt"
return
}
# create a udp server socket
proc udp_listen {port} {
set srv [udp]
fconfigure $srv \
-sockname [list {} $port] \
-blocking 0 \
-buffering none \
-translation binary \
-broadcast 1 \
-reuseaddr 1
fileevent $srv readable [list ::udpEventHandler $srv]
AddMessage "Listening on udp port: [fconfigure $srv -sockname]"
return $srv
}
# create a udp client socket
proc udp_create {host port} {
set s [udp]
fconfigure $s -remote [list $host $port]
fconfigure $s -buffering none -translation binary
return $s
}
proc CreateGui {} {
text .t -yscrollcommand {.s set}
scrollbar .s -command {.t yview}
entry .e -textvariable ::msg
button .ok -text Send -underline 0 -command {SendMessage $::msg}
button .ex -text Exit -underline 1 -command {destroy .}
grid .t - .s -sticky news
grid .e .ok .ex -sticky ew
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
bind .e <Return> {.ok invoke}
}
proc SendMessage {msg} {
variable client
puts $client $msg
}
proc AddMessage {msg} {
if {[string length $msg] > 0} {
.t insert end $msg
}
}
if {!$tcl_interactive} {
CreateGui
set server [udp_listen $Port]
set client [udp_create $Network $Port]
tkwait window .
close $server
close $client
exit 0
}UDP enabled packagesIn tcllib - the dns package is capable of using udp if available. Also the time and ntp packages. [PT]Further note Trivial FTP.
