Internet User Datagram Protocol
#include <sys/socket.h> #include <netinet/in.h> int socket( AF_INET, SOCK_DGRAM, 0 );
UDP is a simple, unreliable datagram protocol that's used to support the SOCK_DGRAM abstraction for the Internet protocol family. UDP sockets are connectionless and are normally used with the sendto() and recvfrom() calls, although you can also use the connect() call to fix the destination for future packets (in which case you can use the recv() or read() and send() or write() system calls).
UDP address formats are identical to those used by TCP. In particular, UDP provides a port identifier in addition to the normal Internet address format. Note that the UDP port space is separate from the TCP port space; that is, a UDP port may not be connected to a TCP port. In addition, broadcast packets may be sent — assuming the underlying network supports this — by using a reserved broadcast address; this address is network-interface dependent.
You can use options at the IP transport level with UDP (see the IP protocol).
A descriptor referencing the socket, or -1 if an error occurs (errno is set).
- You tried to create a socket with a port that has already been allocated.
- You tried to create a socket with a network address for which no network interface exists.
- You tried to establish a connection on a socket that already has one, or to send a datagram with the destination address specified and the socket is already connected.
- The system ran out of memory for an internal data structure.
- You tried to send a datagram, but no destination address was specified and the socket hasn't been connected.