1udp(n) 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.11
12
13 udp_open ?port? ?reuse? ?ipv6?
14
15 udp_conf sock host port
16
17 udp_conf sock ?-myport? ?-remote? ?-peer? ?-broadcast bool? ?-ttl
18 count? ?-mcastadd "groupaddr ?netwif?"? ?-mcastdrop "groupaddr
19 ?netwif?"? ?-mcastgroups? ?-mcastloop bool?
20
21 udp_peek sock ?buffersize?
22
23
25 This package provides support for using UDP through Tcl. The package
26 provides a new channel type and attempts to permit the use of packet
27 oriented UDP over stream oriented Tcl channels. The package defined
28 three commands but udp_conf should be considered depreciated in favour
29 of the standard Tcl command fconfigure.
30
32 udp_open ?port? ?reuse? ?ipv6?
33 udp_open will open a UDP socket. If a port is specified the UDP
34 socket will be opened on that port. Otherwise the system will
35 choose a port and the user can use the udp_conf command to
36 obtain the port number if required.
37
38 The following keywords can be used to specify options on the
39 opened socket.
40
41 reuse Using this keyword sets the SO_REUSEADDR socket option
42 which permits multiple sockets to be bound to the same
43 address/port combination.
44
45 ipv6 By default a IPv4 socket is created. When keyword ipv6 is
46 specified an IPv6 socket is opened.
47
48 udp_conf sock host port
49 Deprecated in favour of the standard Tcl fconfigure command.
50
51 udp_conf in this configuration is used to specify the remote
52 destination for packets written to this sock. You must call this
53 command before writing data to the UDP socket.
54
55 udp_conf sock ?-myport? ?-remote? ?-peer? ?-broadcast bool? ?-ttl
56 count? ?-mcastadd "groupaddr ?netwif?"? ?-mcastdrop "groupaddr
57 ?netwif?"? ?-mcastgroups? ?-mcastloop bool?
58 Deprecated in favour of the standard Tcl fconfigure command.
59
60 In addition to being used to configure the remote host, the
61 udp_conf command is used to obtain information about the UDP
62 socket. NOTE all these options are now available using the stan‐
63 dard Tcl fconfigure command.
64
65 -myport
66 Returns the local port number of the socket.
67
68 -remote
69 Returns the remote hostname and port number as set using
70 udp_conf sock host port.
71
72 -peer Returns the remote hostname and port number for the
73 packet most recently received by this socket.
74
75 -broadcast ?boolean?
76 UDP packets can listen and send on the broadcast address.
77 For some systems a flag must be set on the socket to use
78 broadcast. With no argument this option will return the
79 broadcast setting. With a boolean argument the setting
80 can be modified. This option is not permitted when using
81 IPv6.
82
83 -ttl ?count?
84 The time-to-live is given as the number of router hops
85 the packet may do. For multicast packets this is impor‐
86 tant in specifying the distribution of the packet. The
87 system default for multicast is 1 which restricts the
88 packet to the local subnet. To permit packets to pass
89 routers, you must increase the ttl. A value of 31 should
90 keep it within a site, while 255 is global.
91
92 -mcastadd groupaddr
93
94 -mcastadd "groupaddr netwif"
95
96 -mcastdrop groupaddr
97
98 -mcastdrop "groupaddr netwif"
99
100 -mcastgroups
101 tcludp sockets can support IPv4 and IPv6 multicast opera‐
102 tions. To receive multicast packets the application has
103 to notify the operating system that it should join a par‐
104 ticular multicast group. For IPv4 these are specified as
105 addresses in the range 224.0.0.0 to 239.255.255.255.
106
107 When specifying only the groupaddr the system will deter‐
108 mine the network interface to use. Specifying the netwif
109 will join a multicast group on a specific network inter‐
110 face. This is useful on a multihomed system with multi‐
111 ple network interfaces. On windows you must specify the
112 network interface index. For other platforms the network
113 interface (e.g. 'eth0') name can be specified.
114
115 To view the current set of multicast groups for a channel
116 use -mcastgroups
117
118 -mcastloop ?boolean?
119 With multicast udp the system can choose to receive pack‐
120 ets that it has sent or it can drop them. This is known
121 as multicast loopback and can be controlled using this
122 option. By default the value is true and your application
123 will receive its own transmissions.
124
125 udp_peek sock ?buffersize?
126 Examine a packet without removing it from the buffer. Option
127 buffersize specifies the maximum buffer size. Value must be
128 between 0 and 16.
129
130 This function is not available on windows.
131
133 # Send data to a remote UDP socket
134 proc udp_puts {host port} {
135 set s [udp_open]
136 fconfigure $s -remote [list $host $port]
137 puts $s "Hello, World"
138 close $f
139 }
140
141
142 # A simple UDP server
143 package require udp
144
145 proc udpEventHandler {sock} {
146 set pkt [read $sock]
147 set peer [fconfigure $sock -peer]
148 puts "$peer: [string length $pkt] {$pkt}"
149 return
150 }
151
152 proc udp_listen {port} {
153 set srv [udp_open $port]
154 fconfigure $srv -buffering none -translation binary
155 fileevent $srv readable [list ::udpEventHandler $srv]
156 puts "Listening on udp port: [fconfigure $srv -myport]"
157 return $srv
158 }
159
160 set sock [udp_listen 53530]
161 vwait forever
162 close $sock
163
164
165 # A multicast demo.
166 proc udpEvent {chan} {
167 set data [read $chan]
168 set peer [fconfigure $chan -peer]
169 puts "$peer [string length $data] '$data'"
170 if {[string match "QUIT*" $data]} {
171 close $chan
172 set ::forever 1
173 }
174 return
175 }
176
177 set group 224.5.1.21
178 set port 7771
179 set s [udp_open $port]
180 fconfigure $s -buffering none -blocking 0
181 fconfigure $s -mcastadd $group -remote [list $group $port]
182 fileevent $s readable [list udpEvent $s]
183 puts -nonewline $s "hello, world"
184 set ::forever 0
185 vwait ::forever
186 exit
187
188
190 Some of the code in this extension is copied from Michael Miller's
191 tcludp package.
192 (http://www.neosoft.com/tcl/ftparchive/sorted/comm/tcludp-1.0/) Com‐
193 pared with Michael's UDP extension, this extension provides Windows
194 support and provides the ability of using 'gets/puts' to read/write the
195 socket. In addition, it provides more configuration ability.
196
197 Enhancements to support binary data and to setup the package for the
198 Tcl Extension Architecture by Pat Thoyts.
199
200 Support for IPv6 and allowing a multicast join on a specific network
201 interface is added by Huub Eikens.
202
204 socket(n)
205
207 networking, socket, udp
208
210 Copyright (c) 1999-2000 Columbia University; all rights reserved
211
212
213
214
215Tcl UDP extension 1.0.11 udp(n)