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       net.ipv4.ip_local_port_range and bind the socket to INADDR_ANY.
32       All receive operations return only one  packet.   When  the  packet  is
33       smaller  than  the passed buffer, only that much data is returned; when
34       it is bigger, the packet is truncated and the MSG_TRUNC  flag  is  set.
35       MSG_WAITALL is not supported.
37       IP  options  may be sent or received using the socket options described
38       in ip(7).  They are only processed by the kernel when  the  appropriate
39       /proc  parameter  is enabled (but still passed to the user even when it
40       is turned off).  See ip(7).
42       When the MSG_DONTROUTE flag is set on sending, the destination  address
43       must  refer to a local interface address and the packet is only sent to
44       that interface.
46       By default, Linux UDP does path MTU (Maximum Transmission Unit) discov‐
47       ery.   This  means  the kernel will keep track of the MTU to a specific
48       target IP address and return EMSGSIZE when a UDP packet  write  exceeds
49       it.   When  this  happens,  the  application should decrease the packet
50       size.  Path MTU discovery can be also turned off using the  IP_MTU_DIS‐
51       COVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see
52       ip(7) for details.  When turned off, UDP  will  fragment  outgoing  UDP
53       packets  that  exceed  the interface MTU.  However, disabling it is not
54       recommended for performance and reliability reasons.
56   Address Format
57       UDP uses the IPv4 sockaddr_in address format described in ip(7).
59   Error Handling
60       All fatal errors will be passed to the user as  an  error  return  even
61       when  the  socket  is not connected.  This includes asynchronous errors
62       received from the network.  You may get an error for an earlier  packet
63       that  was  sent  on  the  same socket.  This behavior differs from many
64       other BSD socket implementations which don't pass any errors unless the
65       socket is connected.  Linux's behavior is mandated by RFC 1122.
67       For  compatibility with legacy code, in Linux 2.0 and 2.2 it was possi‐
68       ble to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote  errors
69       only  when  the  socket has been connected (except for EPROTO and EMSG‐
70       SIZE).  Locally generated errors are always passed.  Support  for  this
71       socket  option  was removed in later kernels; see socket(7) for further
72       information.
74       When the IP_RECVERR option is enabled, all errors  are  stored  in  the
75       socket  error  queue,  and  can  be  received  by  recvmsg(2)  with the
76       MSG_ERRQUEUE flag set.
78   /proc interfaces
79       System-wide UDP parameter settings can be  accessed  by  files  in  the
80       directory /proc/sys/net/ipv4/.
82       udp_mem (since Linux 2.6.25)
83              This is a vector of three integers governing the number of pages
84              allowed for queueing by all UDP sockets.
86              min       Below this number of pages, UDP is not bothered  about
87                        its  memory appetite.  When the amount of memory allo‐
88                        cated by UDP exceeds this number, UDP starts to moder‐
89                        ate memory usage.
91              pressure  This  value  was  introduced  to  follow the format of
92                        tcp_mem (see tcp(7)).
94              max       Number of pages allowed for queueing by all UDP  sock‐
95                        ets.
97              Defaults  values  for  these  three items are calculated at boot
98              time from the amount of available memory.
100       udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
101              Minimal size, in bites, of receive buffer used by UDP sockets in
102              moderation.  Each UDP socket is able to use the size for receiv‐
103              ing data, even if total pages  of  UDP  sockets  exceed  udp_mem
104              pressure.
106       udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
107              Minimal  size,  in  bytes, of send buffer used by UDP sockets in
108              moderation.  Each UDP socket is able to use the size for sending
109              data,  even  if  total pages of UDP sockets exceed udp_mem pres‐
110              sure.
112   Socket Options
113       To set or get a UDP socket option, call getsockopt(2) to read  or  set‐
114       sockopt(2)  to  write  the option with the option level argument set to
115       IPPROTO_UDP.
117       UDP_CORK (since Linux 2.5.44)
118              If this option is enabled, then all data output on  this  socket
119              is  accumulated  into a single datagram that is transmitted when
120              the option is disabled.  This option should not be used in  code
121              intended to be portable.
123   Ioctls
124       These ioctls can be accessed using ioctl(2).  The correct syntax is:
126              int value;
127              error = ioctl(udp_socket, ioctl_type, &value);
130              Gets  a  pointer to an integer as argument.  Returns the size of
131              the next pending datagram in the integer in bytes, or 0 when  no
132              datagram is pending.
135              Returns  the number of data bytes in the local send queue.  Only
136              supported with Linux 2.4 and above.
138       In addition all ioctls documented in ip(7) and socket(7) are supported.


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


149       IP_RECVERR is a new feature in Linux 2.2.


152       ip(7), raw(7), socket(7), udplite(7)
154       RFC 768 for the User Datagram Protocol.
155       RFC 1122 for the host requirements.
156       RFC 1191 for a description of path MTU discovery.


159       This page is part of release 3.22 of the Linux  man-pages  project.   A
160       description  of  the project, and information about reporting bugs, can
161       be found at http://www.kernel.org/doc/man-pages/.
165Linux                             2008-11-21                            UDP(7)