1UDP(7)                     Linux Programmer's Manual                    UDP(7)


6       udp - User Datagram Protocol for IPv4


9       #include <sys/socket.h>
10       #include <netinet/in.h>
11       #include <netinet/udp.h>
13       udp_socket = socket(AF_INET, SOCK_DGRAM, 0);


16       This  is  an  implementation of the User Datagram Protocol described in
17       RFC 768.  It implements a connectionless,  unreliable  datagram  packet
18       service.   Packets  may  be reordered or duplicated before they arrive.
19       UDP generates and checks checksums to catch transmission errors.
21       When a UDP socket is  created,  its  local  and  remote  addresses  are
22       unspecified.   Datagrams  can  be  sent  immediately using sendto(2) or
23       sendmsg(2) with a valid destination address as an argument.  When  con‐
24       nect(2) is called on the socket, the default destination address is set
25       and datagrams can now be sent using send(2) or write(2) without  speci‐
26       fying  a  destination  address.   It is still possible to send to other
27       destinations by passing an address  to  sendto(2)  or  sendmsg(2).   In
28       order  to  receive  packets, the socket can be bound to a local address
29       first by using bind(2).  Otherwise, the socket layer will automatically
30       assign   a   free   local   port   out   of   the   range   defined  by
31       /proc/sys/net/ipv4/ip_local_port_range   and   bind   the   socket   to
32       INADDR_ANY.
34       All  receive  operations  return  only  one packet.  When the packet is
35       smaller than the passed buffer, only that much data is  returned;  when
36       it  is  bigger,  the packet is truncated and the MSG_TRUNC flag is set.
37       MSG_WAITALL is not supported.
39       IP options may be sent or received using the socket  options  described
40       in  ip(7).   They are processed by the kernel only when the appropriate
41       /proc parameter is enabled (but still passed to the user even  when  it
42       is turned off).  See ip(7).
44       When  the MSG_DONTROUTE flag is set on sending, the destination address
45       must refer to a local interface address and the packet is sent only  to
46       that interface.
48       By default, Linux UDP does path MTU (Maximum Transmission Unit) discov‐
49       ery.  This means the kernel will keep track of the MTU  to  a  specific
50       target  IP  address and return EMSGSIZE when a UDP packet write exceeds
51       it.  When this happens, the  application  should  decrease  the  packet
52       size.   Path MTU discovery can be also turned off using the IP_MTU_DIS‐
53       COVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see
54       ip(7)  for  details.   When  turned off, UDP will fragment outgoing UDP
55       packets that exceed the interface MTU.  However, disabling  it  is  not
56       recommended for performance and reliability reasons.
58   Address format
59       UDP uses the IPv4 sockaddr_in address format described in ip(7).
61   Error handling
62       All  fatal  errors  will  be passed to the user as an error return even
63       when the socket is not connected.  This  includes  asynchronous  errors
64       received  from the network.  You may get an error for an earlier packet
65       that was sent on the same socket.   This  behavior  differs  from  many
66       other BSD socket implementations which don't pass any errors unless the
67       socket is connected.  Linux's behavior is mandated by RFC 1122.
69       For compatibility with legacy code, in Linux 2.0 and 2.2 it was  possi‐
70       ble  to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors
71       only when the socket has been connected (except for  EPROTO  and  EMSG‐
72       SIZE).   Locally  generated errors are always passed.  Support for this
73       socket option was removed in later kernels; see socket(7)  for  further
74       information.
76       When  the  IP_RECVERR  option  is enabled, all errors are stored in the
77       socket error  queue,  and  can  be  received  by  recvmsg(2)  with  the
78       MSG_ERRQUEUE flag set.
80   /proc interfaces
81       System-wide  UDP  parameter  settings  can  be accessed by files in the
82       directory /proc/sys/net/ipv4/.
84       udp_mem (since Linux 2.6.25)
85              This is a vector of three integers governing the number of pages
86              allowed for queueing by all UDP sockets.
88              min       Below  this number of pages, UDP is not bothered about
89                        its memory appetite.  When the amount of memory  allo‐
90                        cated by UDP exceeds this number, UDP starts to moder‐
91                        ate memory usage.
93              pressure  This value was introduced  to  follow  the  format  of
94                        tcp_mem (see tcp(7)).
96              max       Number  of pages allowed for queueing by all UDP sock‐
97                        ets.
99              Defaults values for these three items  are  calculated  at  boot
100              time from the amount of available memory.
102       udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
103              Minimal  size,  in bytes, of receive buffers used by UDP sockets
104              in moderation.  Each UDP socket is able  to  use  the  size  for
105              receiving  data,  even  if  total  pages  of  UDP sockets exceed
106              udp_mem pressure.
108       udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
109              Minimal size, in bytes, of send buffer used by  UDP  sockets  in
110              moderation.  Each UDP socket is able to use the size for sending
111              data, even if total pages of UDP sockets  exceed  udp_mem  pres‐
112              sure.
114   Socket options
115       To  set  or get a UDP socket option, call getsockopt(2) to read or set‐
116       sockopt(2) to write the option with the option level  argument  set  to
117       IPPROTO_UDP.  Unless otherwise noted, optval is a pointer to an int.
119       UDP_CORK (since Linux 2.5.44)
120              If  this  option is enabled, then all data output on this socket
121              is accumulated into a single datagram that is  transmitted  when
122              the  option is disabled.  This option should not be used in code
123              intended to be portable.
125   Ioctls
126       These ioctls can be accessed using ioctl(2).  The correct syntax is:
128              int value;
129              error = ioctl(udp_socket, ioctl_type, &value);
132              Gets a pointer to an integer as argument.  Returns the  size  of
133              the  next pending datagram in the integer in bytes, or 0 when no
134              datagram is pending.  Warning: Using FIONREAD, it is  impossible
135              to  distinguish  the  case where no datagram is pending from the
136              case where the next pending  datagram  contains  zero  bytes  of
137              data.   It  is  safer  to use select(2), poll(2), or epoll(7) to
138              distinguish these cases.
141              Returns the number of data bytes in the local send queue.   Sup‐
142              ported only with Linux 2.4 and above.
144       In  addition,  all  ioctls  documented  in ip(7) and socket(7) are sup‐
145       ported.


148       All errors documented for socket(7) or ip(7) may be returned by a  send
149       or receive on a UDP socket.
152              No  receiver  was associated with the destination address.  This
153              might be caused by a previous packet sent over the socket.


156       IP_RECVERR is a new feature in Linux 2.2.


159       ip(7), raw(7), socket(7), udplite(7)
161       RFC 768 for the User Datagram Protocol.
162       RFC 1122 for the host requirements.
163       RFC 1191 for a description of path MTU discovery.


166       This page is part of release 4.15 of the Linux  man-pages  project.   A
167       description  of  the project, information about reporting bugs, and the
168       latest    version    of    this    page,    can     be     found     at
169       https://www.kernel.org/doc/man-pages/.
173Linux                             2017-09-15                            UDP(7)