{
    "mode": "man",
    "parameter": "udp",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/udp/7/json",
    "generated": "2026-08-14T04:13:11Z",
    "synopsis": "",
    "sections": {
        "NAME": {
            "content": "udp - User Datagram Protocol for IPv4\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "",
            "subsections": [
                {
                    "name": "#include <sys/socket.h>",
                    "content": ""
                },
                {
                    "name": "#include <netinet/in.h>",
                    "content": ""
                },
                {
                    "name": "#include <netinet/udp.h>",
                    "content": "udpsocket = socket(AFINET, SOCKDGRAM, 0);\n"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "This  is an implementation of the User Datagram Protocol described in RFC 768.  It implements\na connectionless, unreliable datagram packet service.  Packets may be reordered or duplicated\nbefore they arrive.  UDP generates and checks checksums to catch transmission errors.\n\nWhen a UDP socket is created, its local and remote addresses are unspecified.  Datagrams  can\nbe  sent immediately using sendto(2) or sendmsg(2) with a valid destination address as an ar‐\ngument.  When connect(2) is called on the socket, the default destination address is set  and\ndatagrams can now be sent using send(2) or write(2) without specifying a destination address.\nIt  is  still  possible  to  send to other destinations by passing an address to sendto(2) or\nsendmsg(2).  In order to receive packets, the socket can be bound to a local address first by\nusing bind(2).  Otherwise, the socket layer will automatically assign a free local  port  out\nof  the  range  defined  by /proc/sys/net/ipv4/iplocalportrange and bind the socket to IN‐\nADDRANY.\n\nAll receive operations return only one packet.  When the packet is smaller  than  the  passed\nbuffer,  only  that much data is returned; when it is bigger, the packet is truncated and the\nMSGTRUNC flag is set.  MSGWAITALL is not supported.\n\nIP options may be sent or received using the socket options described  in  ip(7).   They  are\nprocessed  by  the  kernel  only  when  the appropriate /proc parameter is enabled (but still\npassed to the user even when it is turned off).  See ip(7).\n\nWhen the MSGDONTROUTE flag is set on sending, the destination address must refer to a  local\ninterface address and the packet is sent only to that interface.\n\nBy  default,  Linux  UDP does path MTU (Maximum Transmission Unit) discovery.  This means the\nkernel will keep track of the MTU to a specific target IP address and return EMSGSIZE when  a\nUDP  packet  write exceeds it.  When this happens, the application should decrease the packet\nsize.  Path MTU discovery can be also turned off using the IPMTUDISCOVER socket  option  or\nthe  /proc/sys/net/ipv4/ipnopmtudisc  file;  see  ip(7) for details.  When turned off, UDP\nwill fragment outgoing UDP packets that exceed the interface MTU.  However, disabling  it  is\nnot recommended for performance and reliability reasons.\n",
            "subsections": [
                {
                    "name": "Address format",
                    "content": "UDP uses the IPv4 sockaddrin address format described in ip(7).\n"
                },
                {
                    "name": "Error handling",
                    "content": "All  fatal  errors  will be passed to the user as an error return even when the socket is not\nconnected.  This includes asynchronous errors received from the network.  You may get an  er‐\nror  for an earlier packet that was sent on the same socket.  This behavior differs from many\nother BSD socket implementations which don't pass any errors unless the socket is  connected.\nLinux's behavior is mandated by RFC 1122.\n\nFor  compatibility  with legacy code, in Linux 2.0 and 2.2 it was possible to set the SOBSD‐\nCOMPAT SOLSOCKET option to receive remote errors only when the  socket  has  been  connected\n(except  for  EPROTO and EMSGSIZE).  Locally generated errors are always passed.  Support for\nthis socket option was removed in later kernels; see socket(7) for further information.\n\nWhen the IPRECVERR option is enabled, all errors are stored in the socket error  queue,  and\ncan be received by recvmsg(2) with the MSGERRQUEUE flag set.\n"
                },
                {
                    "name": "/proc interfaces",
                    "content": "System-wide   UDP   parameter   settings   can   be   accessed  by  files  in  the  directory\n/proc/sys/net/ipv4/.\n\nudpmem (since Linux 2.6.25)\nThis is a vector of three integers governing the number of pages allowed for  queueing\nby all UDP sockets.\n\nmin    Below  this  number  of  pages,  UDP is not bothered about its memory appetite.\nWhen the amount of memory allocated by UDP exceeds this number, UDP  starts  to\nmoderate memory usage.\n\npressure\nThis value was introduced to follow the format of tcpmem (see tcp(7)).\n\nmax    Number of pages allowed for queueing by all UDP sockets.\n\nDefaults  values  for these three items are calculated at boot time from the amount of\navailable memory.\n\nudprmemmin (integer; default value: PAGESIZE; since Linux 2.6.25)\nMinimal size, in bytes, of receive buffers used by UDP sockets  in  moderation.   Each\nUDP  socket  is  able  to  use the size for receiving data, even if total pages of UDP\nsockets exceed udpmem pressure.\n\nudpwmemmin (integer; default value: PAGESIZE; since Linux 2.6.25)\nMinimal size, in bytes, of send buffer used by UDP sockets in  moderation.   Each  UDP\nsocket  is  able  to use the size for sending data, even if total pages of UDP sockets\nexceed udpmem pressure.\n"
                },
                {
                    "name": "Socket options",
                    "content": "To set or get a UDP socket option, call getsockopt(2) to read or setsockopt(2) to  write  the\noption  with the option level argument set to IPPROTOUDP.  Unless otherwise noted, optval is\na pointer to an int.\n\nFollowing is a list of UDP-specific socket options.  For details of some other socket options\nthat are also applicable for UDP sockets, see socket(7).\n\nUDPCORK (since Linux 2.5.44)\nIf this option is enabled, then all data output on this socket is accumulated  into  a\nsingle  datagram  that is transmitted when the option is disabled.  This option should\nnot be used in code intended to be portable.\n\nUDPSEGMENT (since Linux 4.18)\nEnables UDP segmentation offload.  Segmentation offload reduces send(2) cost by trans‐\nferring multiple datagrams worth of data as a single large packet through  the  kernel\ntransmit  path,  even when that exceeds MTU.  As late as possible, the large packet is\nsplit by segment size into a series of datagrams.  This segmentation offload  step  is\ndeferred  to  hardware  if supported, else performed in software.  This option takes a\nvalue in the range [0, USHRTMAX] that sets the segment size:  the  size  of  datagram\npayload,  excluding the UDP header.  The segment size must be chosen such that at most\n64 datagrams are sent in a single call and that the datagrams after segmentation  meet\nthe same MTU rules that apply to datagrams sent without this option.  Segmentation of‐\nfload  depends on checksum offload, as datagram checksums are computed after segmenta‐\ntion.  The option may also be set for individual sendmsg(2) calls by passing it  as  a\ncmsg(3).   A  value  of  zero disables the feature.  This option should not be used in\ncode intended to be portable.\n\nUDPGRO (since Linux 5.0)\nEnables UDP receive offload.  If enabled, the socket may  receive  multiple  datagrams\nworth of data as a single large buffer, together with a cmsg(3) that holds the segment\nsize.  This option is the inverse of segmentation offload.  It reduces receive cost by\nhandling  multiple  datagrams worth of data as a single large packet in the kernel re‐\nceive path, even when that exceeds MTU.  This option should not be used  in  code  in‐\ntended to be portable.\n"
                },
                {
                    "name": "Ioctls",
                    "content": "These ioctls can be accessed using ioctl(2).  The correct syntax is:\n\nint value;\nerror = ioctl(udpsocket, ioctltype, &value);\n\nFIONREAD (SIOCINQ)\nGets  a pointer to an integer as argument.  Returns the size of the next pending data‐\ngram in the integer in bytes, or 0 when no datagram is pending.  Warning: Using  FION‐\nREAD,  it  is impossible to distinguish the case where no datagram is pending from the\ncase where the next pending datagram contains zero bytes of data.  It is safer to  use\nselect(2), poll(2), or epoll(7) to distinguish these cases.\n\nTIOCOUTQ (SIOCOUTQ)\nReturns  the  number of data bytes in the local send queue.  Supported only with Linux\n2.4 and above.\n\nIn addition, all ioctls documented in ip(7) and socket(7) are supported.\n"
                }
            ]
        },
        "ERRORS": {
            "content": "All errors documented for socket(7) or ip(7) may be returned by a send or receive  on  a  UDP\nsocket.\n\nECONNREFUSED\nNo  receiver  was  associated with the destination address.  This might be caused by a\nprevious packet sent over the socket.\n",
            "subsections": []
        },
        "VERSIONS": {
            "content": "IPRECVERR is a new feature in Linux 2.2.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "ip(7), raw(7), socket(7), udplite(7)\n\nThe kernel source file Documentation/networking/ip-sysctl.txt.\n\nRFC 768 for the User Datagram Protocol.\nRFC 1122 for the host requirements.\nRFC 1191 for a description of path MTU discovery.\n\nLinux man-pages 6.7                          2023-10-31                                       udp(7)",
            "subsections": []
        }
    },
    "summary": "udp - User Datagram Protocol for IPv4",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "ip",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/ip/7/json"
        },
        {
            "name": "raw",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/raw/7/json"
        },
        {
            "name": "socket",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/socket/7/json"
        },
        {
            "name": "udplite",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/udplite/7/json"
        }
    ]
}