1udp(n) Tcl UDP extension udp(n)
2
3
4
6 udp - Create UDP sockets in Tcl
7
9 package require Tcl 8.2
10
11 package require udp 1.0
12
13 udp_open ?port?
14
15 udp_conf sock host port
16
17 udp_conf sock ?-myport? ?-remote? ?-peer? ?-broadcast bool? ?-ttl
18 count?
19
20 udp_conf ?-mcastadd groupaddr?
21
22 udp_conf ?-mcastdrop groupaddr?
23
24 udp_peek sock ?buffersize?
25
26
28 This package provides support for using UDP through Tcl. The package
29 provides a new channel type and attempts to permit the use of packet
30 oriented UDP over stream oriented Tcl channels. The package defined
31 three commands but udp_conf should be considered depreciated in favour
32 of the standard Tcl command fconfigure.
33
35 udp_open ?port?
36 udp_open will open a UDP socket. If port is specified the UDP
37 socket will be opened on that port. Otherwise the system will
38 choose a port and the user can use the udp_conf command to
39 obtain the port number if required.
40
41 udp_conf sock host port
42 udp_conf in this configuration is used to specify the remote
43 destination for packets written to this sock. You must call this
44 command before writing data to the UDP socket.
45
46 udp_conf sock ?-myport? ?-remote? ?-peer? ?-broadcast bool? ?-ttl
47 count?
48 In addition to being used to configure the remote host, the
49 udp_conf command is used to obtain information about the UDP
50 socket.
51
52 -myport
53 Returns the local port number of the socket.
54
55 -remote
56 Returns the remote hostname and port number as set using
57 udp_conf sock host port.
58
59 -peer Returns the remote hostname and port number for the
60 packet most recently received by this socket.
61
62 -broadcast ?boolean?
63 UDP packets can listen and send on the broadcast address.
64 For some systems a flag must be set on the socket to use
65 broadcast. With no argument this option will return the
66 broadcast setting. With a boolean argument the setting
67 can be modified.
68
69 -ttl ?count?
70 The time-to-live is given as the number of router hops
71 the packet may do. For multicast packets this is impor‐
72 tant in specifying the distribution of the packet. The
73 system default for multicast is 1 which restricts the
74 packet to the local subnet. To permit packets to pass
75 routers, you must increase the ttl. A value of 31 should
76 keep it within a site, while 255 is global.
77
78
79 udp_conf ?-mcastadd groupaddr?
80
81 udp_conf ?-mcastdrop groupaddr?
82 tcludp sockets can support IPv4 multicast operations. To recieve
83 multicast packets the application has to notify the operating
84 system that it should join a particular multicast group. These
85 are specified as addresses in the range 224.0.0.0 to
86 239.255.255.255.
87
88 udp_peek sock ?buffersize?
89 Examine a packet without removing it from the buffer. This
90 function is not available on windows.
91
93 # Send data to a remote UDP socket
94 proc udp_puts {host port} {
95 set s [udp_open]
96 fconfigure $s -remote [list $host $port]
97 puts $s "Hello, World"
98 close $f
99 }
100
101
102 # A simple UDP server
103 package require udp
104
105 proc udpEventHandler {sock} {
106 set pkt [read $sock]
107 set peer [fconfigure $sock -peer]
108 puts "$peer: [string length $pkt] {$pkt}"
109 return
110 }
111
112 proc udp_listen {port} {
113 set srv [udp_open $port]
114 fconfigure $srv -buffering none -translation binary
115 fileevent $srv readable [list ::udpEventHandler $srv]
116 puts "Listening on udp port: [fconfigure $srv -myport]"
117 return $srv
118 }
119
120 set sock [udp_listen 53530]
121 vwait forever
122 close $sock
123
124
125 # A multicast demo.
126 proc udpEvent {chan} {
127 set data [read $chan]
128 set peer [fconfigure $chan -peer]
129 puts "$peer [string length $data] '$data'"
130 if {[string match "QUIT*" $data]} {
131 close $chan
132 set ::forever 1
133 }
134 return
135 }
136
137 set group 224.5.1.21
138 set port 7771
139 set s [udp_open $port]
140 fconfigure $s -buffering none -blocking 0
141 fconfigure $s -mcastadd $group -remote [list $group $port]
142 fileevent $s readable [list udpEvent $s]
143 puts -nonewline $s "hello, world"
144 set ::forever 0
145 vwait ::forever
146 exit
147
148
150 Some of the code in this extension is copied from Michael Miller's
151 tcludp package.
152 (http://www.neosoft.com/tcl/ftparchive/sorted/comm/tcludp-1.0/) Com‐
153 pared with Michael's UDP extension, this extension provides Windows
154 support and provides the ability of using 'gets/puts' to read/write the
155 socket. In addition, it provides more configuration ability.
156
157 Enhancements to support binary data and to setup the package for the
158 Tcl Extension Architecture by Pat Thoyts.
159
161 socket(n)
162
164 networking, socket, udp
165
167 Copyright (c) 1999-2000 Columbia University; all rights reserved
168
169
170
171
172udp 1.0.7 udp(n)