| Red Hat Documentation: man pages: man7: Linux Programmer's Manual (7) |
UDPSection: Linux Programmer's Manual (7)Updated: 2 Oct 2025 Index NAMEudp - UDP protocol on top of IPv4.SYNOPSIS#include <sys/socket.h>#include <netinet/in.h> udp_socket = socket(PF_INET, SOCK_DGRAM, 0); DESCRIPTIONThis is an implemention of the User Datagram Protocol described in RFC768. 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 or with a valid destination address as an argument. When is called on the socket the default destination address is set and datagrams can now be sent using or without specifying an destination address. It is still possible to send to other destinations by passing an address to or In order to receive packets the socket should be bound to an local address first by using when this is not the case the socket layer will automatically assign a local port on the first user receive request. 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. IP options may be sent or received using the socket options described in They are only processed by the kernel when the appropriate sysctl is enabled (but still passed to the user even when it is turned off). See When the MSG_DONTROUTE flag is set on sending the destination address must refer to an local interface address and the packet is only sent to that interface. UDP fragments a packet when its total length exceeds the interface MTU (Maximum Transmission Unit). A more network friendly alternative is to use path MTU discovery as described in the IP_PMTU_DISCOVER section of ADDRESS FORMATUDP uses the IPv4 sockaddr_in address format described inERROR HANDLINGAll fatal errors will be passed to the user as an error return even when the socket is not connected. This behaviour differs from many other BSD socket implementations which don't pass any errors unless the socket is connected. Linux's behaviour is mandated by RFC1122.For compatibility with legacy code it is possible to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors only when the socket has been connected (except for EPROTO and EMSGSIZE). It is better to fix the code to handle errors properly than to enable this option. Locally generated errors are always passed. When the IP_RECVERR option is enabled all errors are stored in the socket error queue and can be received by with the MSG_ERRQUEUE flag set. ERRORSAll errors documented for or 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. VERSIONSIP_RECVERR is a new feature in Linux 2.2CREDITSThis man page was writen by Andi Kleen.SEE ALSO
RFC768 for the User Datagram protocol.
IndexThis document was created by using the manual pages. Time: 12:14:04 GMT, March 24, 2025 |
|
© 2001 Red Hat, Inc. All rights reserved. About Red Hat : Legal statement : Privacy statement : Y2K statement : Contact Red Hat |