# man > udp(7)

[_udp_(7)](https://www.chedong.com/phpMan.php/man/udp/7/markdown)                            Miscellaneous Information Manual                            [_udp_(7)](https://www.chedong.com/phpMan.php/man/udp/7/markdown)

## NAME
       udp - User Datagram Protocol for IPv4

## SYNOPSIS
### #include <sys/socket.h>
### #include <netinet/in.h>
### #include <netinet/udp.h>

       _udp_socket_ **= socket(AF_INET, SOCK_DGRAM, 0);**

## DESCRIPTION
       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 [**sendto**(2)](https://www.chedong.com/phpMan.php/man/sendto/2/markdown) or [**sendmsg**(2)](https://www.chedong.com/phpMan.php/man/sendmsg/2/markdown) with a valid destination address as an ar‐
       gument.  When [**connect**(2)](https://www.chedong.com/phpMan.php/man/connect/2/markdown) is called on the socket, the default destination address is set  and
       datagrams can now be sent using [**send**(2)](https://www.chedong.com/phpMan.php/man/send/2/markdown) or [**write**(2)](https://www.chedong.com/phpMan.php/man/write/2/markdown) without specifying a destination address.
       It  is  still  possible  to  send to other destinations by passing an address to [**sendto**(2)](https://www.chedong.com/phpMan.php/man/sendto/2/markdown) or
       [**sendmsg**(2)](https://www.chedong.com/phpMan.php/man/sendmsg/2/markdown).  In order to receive packets, the socket can be bound to a local address first by
       using [**bind**(2)](https://www.chedong.com/phpMan.php/man/bind/2/markdown).  Otherwise, the socket layer will automatically assign a free local  port  out
       of  the  range  defined  by _/proc/sys/net/ipv4/ip_local_port_range_ and bind the socket to **IN‐**
       **ADDR_ANY**.

       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
       **MSG_TRUNC **flag is set.  **MSG_WAITALL **is not supported.

       IP options may be sent or received using the socket options described  in  [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown).   They  are
       processed  by  the  kernel  only  when  the appropriate _/proc_ parameter is enabled (but still
       passed to the user even when it is turned off).  See [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown).

       When the **MSG_DONTROUTE **flag is set on sending, the destination address must refer to a  local
       interface address and the packet is sent only 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 **EMSGSIZE **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 **IP_MTU_DISCOVER **socket  option  or
       the  _/proc/sys/net/ipv4/ip_no_pmtu_disc_  file;  see  [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown) for details.  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.

### Address format
       UDP uses the IPv4 _sockaddr_in_ address format described in [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown).

### Error handling
       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  er‐
       ror  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 **RFC 1122**.

       For  compatibility  with legacy code, in Linux 2.0 and 2.2 it was possible to set the **SO_BSD‐**
       **COMPAT SOL_SOCKET **option to receive remote errors only when the  socket  has  been  connected
       (except  for  **EPROTO **and **EMSGSIZE**).  Locally generated errors are always passed.  Support for
       this socket option was removed in later kernels; see [**socket**(7)](https://www.chedong.com/phpMan.php/man/socket/7/markdown) for further information.

       When the **IP_RECVERR **option is enabled, all errors are stored in the socket error  queue,  and
       can be received by [**recvmsg**(2)](https://www.chedong.com/phpMan.php/man/recvmsg/2/markdown) with the **MSG_ERRQUEUE **flag set.

### /proc interfaces
       System-wide   UDP   parameter   settings   can   be   accessed  by  files  in  the  directory
       _/proc/sys/net/ipv4/_.

       _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.

              _min_    Below  this  number  of  pages,  UDP is not bothered about its memory appetite.
                     When the amount of memory allocated by UDP exceeds this number, UDP  starts  to
                     moderate memory usage.

              _pressure_
                     This value was introduced to follow the format of _tcp_mem_ (see [**tcp**(7)](https://www.chedong.com/phpMan.php/man/tcp/7/markdown)).

              _max_    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 bytes, of receive buffers 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 _udp_mem_ pressure.

### Socket options
       To set or get a UDP socket option, call [**getsockopt**(2)](https://www.chedong.com/phpMan.php/man/getsockopt/2/markdown) to read or [**setsockopt**(2)](https://www.chedong.com/phpMan.php/man/setsockopt/2/markdown) to  write  the
       option  with the option level argument set to **IPPROTO_UDP**.  Unless otherwise noted, _optval_ is
       a pointer to an _int_.

       Following is a list of UDP-specific socket options.  For details of some other socket options
       that are also applicable for UDP sockets, see [**socket**(7)](https://www.chedong.com/phpMan.php/man/socket/7/markdown).

       **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 portable.

       **UDP_SEGMENT **(since Linux 4.18)
              Enables UDP segmentation offload.  Segmentation offload reduces [**send**(2)](https://www.chedong.com/phpMan.php/man/send/2/markdown) cost by trans‐
              ferring multiple datagrams worth of data as a single large packet through  the  kernel
              transmit  path,  even when that exceeds MTU.  As late as possible, the large packet is
              split by segment size into a series of datagrams.  This segmentation offload  step  is
              deferred  to  hardware  if supported, else performed in software.  This option takes a
              value in the range [**0**, **USHRT_MAX**] that sets the segment size:  the  size  of  datagram
              payload,  excluding the UDP header.  The segment size must be chosen such that at most
              64 datagrams are sent in a single call and that the datagrams after segmentation  meet
              the same MTU rules that apply to datagrams sent without this option.  Segmentation of‐
              fload  depends on checksum offload, as datagram checksums are computed after segmenta‐
              tion.  The option may also be set for individual [**sendmsg**(2)](https://www.chedong.com/phpMan.php/man/sendmsg/2/markdown) calls by passing it  as  a
              [**cmsg**(3)](https://www.chedong.com/phpMan.php/man/cmsg/3/markdown).   A  value  of  zero disables the feature.  This option should not be used in
              code intended to be portable.

       **UDP_GRO **(since Linux 5.0)
              Enables UDP receive offload.  If enabled, the socket may  receive  multiple  datagrams
              worth of data as a single large buffer, together with a [**cmsg**(3)](https://www.chedong.com/phpMan.php/man/cmsg/3/markdown) that holds the segment
              size.  This option is the inverse of segmentation offload.  It reduces receive cost by
              handling  multiple  datagrams worth of data as a single large packet in the kernel re‐
              ceive path, even when that exceeds MTU.  This option should not be used  in  code  in‐
              tended to be portable.

### Ioctls
       These ioctls can be accessed using [**ioctl**(2)](https://www.chedong.com/phpMan.php/man/ioctl/2/markdown).  The correct syntax is:

              **int _**value_**;**
              _error_ **= ioctl(_**udp_socket_**, _**ioctl_type_**, &_**value_**);**

       **FIONREAD **(**SIOCINQ**)
              Gets  a pointer to an integer as argument.  Returns the size of the next pending data‐
              gram in the integer in bytes, or 0 when no datagram is pending.  **Warning: **Using  **FION‐**
              **READ**,  it  is impossible to distinguish the case where no datagram is pending from the
              case where the next pending datagram contains zero bytes of data.  It is safer to  use
              [**select**(2)](https://www.chedong.com/phpMan.php/man/select/2/markdown), [**poll**(2)](https://www.chedong.com/phpMan.php/man/poll/2/markdown), or [**epoll**(7)](https://www.chedong.com/phpMan.php/man/epoll/7/markdown) to distinguish these cases.

       **TIOCOUTQ **(**SIOCOUTQ**)
              Returns  the  number of data bytes in the local send queue.  Supported only with Linux
              2.4 and above.

       In addition, all ioctls documented in [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown) and [**socket**(7)](https://www.chedong.com/phpMan.php/man/socket/7/markdown) are supported.

## ERRORS
       All errors documented for [**socket**(7)](https://www.chedong.com/phpMan.php/man/socket/7/markdown) or [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown) may be returned by a send or receive  on  a  UDP
       socket.

       **ECONNREFUSED**
              No  receiver  was  associated with the destination address.  This might be caused by a
              previous packet sent over the socket.

## VERSIONS
       **IP_RECVERR **is a new feature in Linux 2.2.

## SEE ALSO
       [**ip**(7)](https://www.chedong.com/phpMan.php/man/ip/7/markdown), [**raw**(7)](https://www.chedong.com/phpMan.php/man/raw/7/markdown), [**socket**(7)](https://www.chedong.com/phpMan.php/man/socket/7/markdown), [**udplite**(7)](https://www.chedong.com/phpMan.php/man/udplite/7/markdown)

       The kernel source file _Documentation/networking/ip-sysctl.txt_.

       RFC 768 for the User Datagram Protocol.
       RFC 1122 for the host requirements.
       RFC 1191 for a description of path MTU discovery.

Linux man-pages 6.7                          2023-10-31                                       [_udp_(7)](https://www.chedong.com/phpMan.php/man/udp/7/markdown)
