15.3.2 UDP
(require racket/udp) | package: base |
For information about UDP in general, see TCP/IP Illustrated, Volume 1 by W. Richard Stevens.
procedure
(udp-open-socket [ family-hostname family-port-no]) → udp? family-hostname : (or/c string? #f) = #f family-port-no : (or/c port-number? #f) = #f
If family-hostname or family-port-no is not #f, then the socket’s protocol family is determined from these arguments. The socket is not bound to the hostname or port number. For example, the arguments might be the hostname and port to which messages will be sent through the socket, which ensures that the socket’s protocol family is consistent with the destination. Alternately, the arguments might be the same as for a future call to udp-bind!, which ensures that the socket’s protocol family is consistent with the binding. If neither family-hostname nor family-port-no is non-#f, then the socket’s protocol family is IPv4.
On variants of Unix and MacOS that support FD_CLOEXEC, a socket is given that flag so that it is not shared with a subprocess created by subprocess.
Changed in version 8.11.1.6 of package base: Changed to use FD_CLOEXEC where supported by the operating system.
procedure
(udp-bind! udp-socket hostname-string port-no [ reuse?]) → void? udp-socket : udp? hostname-string : (or/c string? #f) port-no : listen-port-number? reuse? : any/c = #f
If hostname-string is #f, then the socket accepts connections to all of the listening machine’s IP addresses at port-no. Otherwise, the socket accepts connections only at the IP address associated with the given name. For example, providing "127.0.0.1" as hostname-string typically creates a listener that accepts only connections to "127.0.0.1" from the local machine.
A socket cannot receive datagrams until it is bound to a local address and port. If a socket is not bound before it is used with a sending procedure udp-send, udp-send-to, etc., the sending procedure binds the socket to a random local port. Similarly, if an event from udp-send-evt or udp-send-to-evt is chosen for a synchronization (see Events), the socket is bound; if the event is not chosen, the socket may or may not become bound.
The binding of a bound socket cannot be changed, with one exception: on some systems, if the socket is bound automatically when sending, if the socket is disconnected via udp-connect!, and if the socket is later used again in a send, then the later send may change the socket’s automatic binding.
If udp-socket is already bound or closed, the exn:fail:network exception is raised.
If the reuse? argument is true, then udp-bind! will set the SO_REUSEADDR socket option before binding, permitting the sharing of access to a UDP port between many processes on a single machine when using UDP multicast.
procedure
(udp-connect! udp-socket hostname-string port-no) → void? udp-socket : udp? hostname-string : (or/c string? #f) port-no : (or/c port-number? #f)
If hostname-string is #f, then port-no also must be #f, and the port is disconnected (if connected). If one of hostname-string or port-no is #f and the other is not, the exn:fail:contract exception is raised.
A connected socket can be used with udp-send (not udp-send-to), and it accepts datagrams only from the connected address and port. A socket need not be connected to receive datagrams. A socket can be connected, re-connected, and disconnected any number of times.
If udp-socket is closed, the exn:fail:network exception is raised.
procedure
(udp-send-to udp-socket hostname port-no bstr [ start-pos end-pos]) → void? udp-socket : udp? hostname : string? port-no : port-number? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
If start-pos is greater than the length of bstr, or if end-pos is less than start-pos or greater than the length of bstr, the exn:fail:contract exception is raised.
If udp-socket is closed or connected, the exn:fail:network exception is raised.
procedure
udp-socket : udp? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-send-to* udp-socket hostname port-no bstr [ start-pos end-pos]) → boolean? udp-socket : udp? hostname : string? port-no : port-number? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
udp-socket : udp? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-send-to/enable-break udp-socket hostname port-no bstr [ start-pos end-pos]) → void? udp-socket : udp? hostname : string? port-no : port-number? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-send/enable-break udp-socket bstr [ start-pos end-pos]) → void? udp-socket : udp? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-receive! udp-socket bstr [ start-pos end-pos]) →
exact-nonnegative-integer? string? port-number? udp-socket : udp? bstr : (and/c bytes? (not immutable?)) start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
Three values are returned: the number of received bytes (between 0 and end-pos-start-pos, a hostname string indicating the source address of the datagram, and an integer indicating the source port of the datagram. If the received datagram is longer than end-pos-start-pos bytes, the remainder is discarded.
If start-pos is greater than the length of bstr, or if end-pos is less than start-pos or greater than the length of bstr, the exn:fail:contract exception is raised.
procedure
(udp-receive!* udp-socket bstr [ start-pos end-pos])
→
(or/c exact-nonnegative-integer? #f) (or/c string? #f) (or/c port-number? #f) udp-socket : udp? bstr : (and/c bytes? (not immutable?)) start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-receive!/enable-break udp-socket bstr [ start-pos end-pos])
→
exact-nonnegative-integer? string? port-number? udp-socket : udp? bstr : (and/c bytes? (not immutable?)) start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-set-receive-buffer-size! udp-socket size) → void? udp-socket : udp? size : exact-positive-integer?
If size is greater than the maximum allowed by the system, the exn:fail:network exception is raised.
Added in version 7.1.0.11 of package base.
procedure
(udp-bound? udp-socket) → boolean?
udp-socket : udp?
procedure
(udp-connected? udp-socket) → boolean?
udp-socket : udp?
procedure
(udp-send-ready-evt udp-socket) → evt?
udp-socket : udp?
procedure
(udp-receive-ready-evt udp-socket) → evt?
udp-socket : udp?
procedure
(udp-send-to-evt udp-socket hostname port-no bstr [ start-pos end-pos]) → evt? udp-socket : udp? hostname : string? port-no : port-number? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-send-evt udp-socket bstr [ start-pos end-pos]) → evt? udp-socket : udp? bstr : bytes? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-receive!-evt udp-socket bstr [ start-pos end-pos]) → evt? udp-socket : udp? bstr : (and/c bytes? (not immutable?)) start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (bytes-length bstr)
procedure
(udp-addresses udp-port [port-numbers?])
→
(or/c (values string? string?) (values string? listen-port-number? string? listen-port-number?)) udp-port : udp? port-numbers? : any/c = #f
If port-numbers? is true, then four results are returned: a string for the local machine’s address, an exact integer between 1 and 65535 for the local machine’s port number or 0 if the socket is unbound, a string for the remote machine’s address, and an exact integer between 1 and 65535 for the remote machine’s port number or 0 if the socket is unconnected.
If the given port has been closed, the exn:fail:network exception is raised.
procedure
(udp-set-ttl! udp-socket ttl) → void?
udp-socket : udp? ttl : byte?
procedure
udp-socket : udp?
Sets or retrieves the current time-to-live setting of udp-socket.
Added in version 7.5.0.5 of package base.
procedure
(udp-multicast-join-group! udp-socket multicast-addr hostname) → void? udp-socket : udp? multicast-addr : string? hostname : (or/c string? #f)
procedure
(udp-multicast-leave-group! udp-socket multicast-addr hostname) → void? udp-socket : udp? multicast-addr : string? hostname : (or/c string? #f)
The multicast-addr argument must be a valid IPv4 multicast IP address; for example, "224.0.0.251" is the appropriate address for the mDNS protocol. The hostname argument selects the interface that the socket uses to receive (not send) multicast datagrams; if hostname is #f or "0.0.0.0", the kernel selects an interface automatically.
Leaving a group requires the same multicast-addr and hostname arguments that were used to join the group.
procedure
(udp-multicast-interface udp-socket) → string?
udp-socket : udp?
procedure
(udp-multicast-set-interface! udp-socket hostname) → void? udp-socket : udp? hostname : (or/c string? #f)
procedure
(udp-multicast-set-loopback! udp-socket loopback?) → void? udp-socket : udp? loopback? : any/c
procedure
(udp-multicast-loopback? udp-socket) → boolean?
udp-socket : udp?
Sets or checks whether udp-socket receives its own multicast datagrams: a #t result or a true value for loopback? indicates that self-receipt is enabled, and #f indicates that self-receipt is disabled.
procedure
(udp-multicast-set-ttl! udp-socket ttl) → void?
udp-socket : udp? ttl : byte?
procedure
(udp-multicast-ttl udp-socket) → byte?
udp-socket : udp?
Sets or retrieves the current time-to-live setting of udp-socket.
The time-to-live setting should almost always be 1, and it is important that this number is as low as possible. In fact, these functions seldom should be used at all. See the documentation for your platform’s IP stack.