This is an implementation of the User Datagram Protocol
described in RFC 768.
It implements a connectionless, unreliable datagram packet service.
Packets may be reordered or duplicated before they arrive.
UDP generates and checks checksums to catch transmission errors.
When a UDP socket is created,
its local and remote addresses are unspecified.
Datagrams can be sent immediately using
with a valid destination address as an argument.
is called on the socket, the default destination address is set and
datagrams can now be sent using
without specifying a destination address.
It is still possible to send to other destinations by passing an
In order to receive packets, the socket can be bound to a local
address first by using
Otherwise the socket layer will automatically assign
a free local port out of the range defined by
and bind the socket to
All receive operations return only one packet.
When the packet is smaller than the passed buffer, only that much
data is returned; when it is bigger, the packet is truncated and the
flag is set.
is not supported.
IP options may be sent or received using the socket options described in
They are only processed by the kernel when the appropriate
is enabled (but still passed to the user even when it is turned off).
flag is set on sending, the destination address must refer to a local
interface address and the packet is only sent to that interface.
By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery.
This means the kernel
will keep track of the MTU to a specific target IP address and return
when a UDP packet write exceeds it.
When this happens, the application should decrease the packet size.
Path MTU discovery can be also turned off using the
socket option or the
When turned off, UDP will fragment outgoing UDP packets
that exceed the interface MTU.
However, disabling it is not recommended
for performance and reliability reasons.
UDP uses the IPv4
address format described in
All fatal errors will be passed to the user as an error return even
when the socket is not connected.
This includes asynchronous errors
received from the network.
You may get an error for an earlier packet
that was sent on the same socket.
This behavior differs from many other BSD socket implementations
which don't pass any errors unless the socket is connected.
Linux's behavior is mandated by
For compatibility with legacy code, in Linux 2.0 and 2.2
it was possible to set the
option to receive remote errors only when the socket has been
connected (except for
Locally generated errors are always passed.
Support for this socket option was removed in later kernels; see
for further information.
option is enabled, all errors are stored in the socket error queue,
and can be received by
System-wide UDP parameter settings can be accessed by files in the directory
udp_mem (since Linux 2.6.25)
This is a vector of three integers governing the number
of pages allowed for queueing by all UDP sockets.
Below this number of pages, UDP is not bothered about its
When the amount of memory allocated by UDP exceeds
this number, UDP starts to moderate memory usage.
This value was introduced to follow the format of
Number of pages allowed for queueing by all UDP sockets.
Defaults values for these three items are
calculated at boot time from the amount of available memory.
udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
Minimal size, in bites, of receive buffer used by UDP sockets in moderation.
Each UDP socket is able to use the size for receiving data,
even if total pages of UDP sockets exceed udp_mem pressure.
udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
Minimal size, in bytes, of send buffer used by UDP sockets in moderation.
Each UDP socket is able to use the size for sending data,
even if total pages of UDP sockets exceed
To set or get a UDP socket option, call
to read or
to write the option with the option level argument set to
UDP_CORK (since Linux 2.5.44)
If this option is enabled, then all data output on this socket
is accumulated into a single datagram that is transmitted when
the option is disabled.
This option should not be used in code intended to be
These ioctls can be accessed using
The correct syntax is:
int value;error = ioctl(udp_socket, ioctl_type, &value);
Gets a pointer to an integer as argument.
Returns the size of the next pending datagram in the integer in bytes,
or 0 when no datagram is pending.
Returns the number of data bytes in the local send queue.
Only supported with Linux 2.4 and above.
In addition all ioctls documented in
All errors documented for
may be returned by a send or receive on a UDP socket.
No receiver was associated with the destination address.
This might be caused by a previous packet sent over the socket.