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>
12       udp_socket = socket(AF_INET, SOCK_DGRAM, 0);


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


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


154       IP_RECVERR is a new feature in Linux 2.2.


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


164       This  page  is  part of release 3.25 of the Linux man-pages project.  A
165       description of the project, and information about reporting  bugs,  can
166       be found at http://www.kernel.org/doc/man-pages/.
170Linux                             2010-06-13                            UDP(7)