User Datagram Protocol
The
User Datagram Protocol (
UDP) is one of the core protocols of the
Internet protocol suite. Using UDP, programs on networked computers can send short messages sometimes known as
datagrams to one another.
UDP does not provide the reliability and ordering guarantees that
TCP does. Datagrams may arrive out of order or go missing without notice. Without the overhead of checking if every packet actually arrived, UDP is faster and more efficient for many lightweight or time-sensitive purposes. Also, its stateless nature is useful for servers that answer small queries from huge numbers of clients. Compared to
TCP, UDP is required for
broadcast (send to all on local network) and
multicast (send to all subscribers).
Common network applications that use UDP include the
Domain Name System (DNS),
streaming media applications,
Voice over IP,
Trivial File Transfer Protocol (TFTP),
BitTorrent and
online games.
UDP utilizes
ports to allow application-to-application communication.The port field is 16-
bits so the valid range is 0 to 65,535.Port 0 is reserved, but is a permissible source port value if the sending process does not expect messages in response.
Ports 1 through 1023 are named "well-known" ports and on
Unix-derived
operating systems binding to one of these ports requires
root access.
Ports 1024 through 49,151 are
registered ports.
Ports 49,152 through 65,535 are
ephemeral ports and are used as temporary ports primarily by
clients when communicating to
servers.
UDP is a minimal message-orientated
transport layer protocol that is currently documented in
IETF RFC 768.
In the
Internet protocol suite, UDP provides a very simple interface between a
network layer below (e.g.,
IPv4) and a
session layer or
application layer above.
UDP provides no guarantees to the
upper layer protocol for message delivery and a UDP sender retains no state on UDP messages once sent (for this reason UDP is sometimes called the
Unreliable Datagram Protocol).UDP adds only application
multiplexing and
checksumming of the header and payload. If any kind of reliability for the information transmited is needed, it must be implemented in upper layers.
| + | Bits 0 - 15 | 16 - 31 |
|---|
| 0 | Source Port | Destination Port |
|---|
| 32 | Length | Checksum |
|---|
| 64 | Data |
|---|
The UDP header consists of only 4 fields of which two are optional (red background in table).
; Source port : This field identifies the sending port when meaningful and should be assumed to be the port to reply to if needed. If not used then it should be zero.
; Destination port : This field identifies the destination port and is required.
; Length : A 16-bit field that specifies the length of the entire datagram: header and data. The minimum length is 8 bytes since that's the length of the header.
; Checksum : The 16-bit
checksum field is used for error-checking of the header
and data. The method used to compute the checksum is defined within RFC 768:
:
Checksum is the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the UDP header, and the data, padded with zero octets at the end (if necessary) to make a multiple of two octets.In other words, all 16-bit words are summed together using
one's complement (with the checksum field set to zero). The sum is then one's complemented. This final value is then inserted as the checksum field. Algorithmically speaking, this is the same as for
IPv4.
The difference is in the data used to make the checksum. Included is a pseudo-header that mimics the IP header:
| + | Bits 0 - 7 | 8 - 15 | 16 - 23 | 24 - 31 |
|---|
| 0 | Source address |
|---|
| 32 | Destination address |
|---|
| 64 | Zeros | Protocol | UDP length |
|---|
| 96 | Source Port | Destination Port |
|---|
| 128 | Length | Checksum |
|---|
| 160 | Data |
|---|
The source and destination addresses are those in the IPv4 header. The protocol is that for UDP (
see List of IPv4 protocol numbers): 17. The UDP length field is the length of the UDP header and data.
If the checksum is calculated to be zero (all 0's) it should be sent as negative zero (all 1's). If a checksum is not used it should be sent as zero (all 0's) as zero indicates an unused checksum.
Lacking reliability, UDP applications must generally be willing to accept some loss, errors or duplication. Some applications such as
TFTP may add rudimentary reliability mechanisms into the application layer as needed. Most often, UDP applications do not require reliability mechanisms and may even be hindered by them.
Streaming media, real-time multiplayer games and
voice over IP (VoIP) are examples of applications that often use UDP. If an application requires a high degree of reliability, a protocol such as the
Transmission Control Protocol or
erasure codes may be used instead.
Lacking any congestion avoidance and control mechanisms, network-based mechanisms are required to minimize potential congestion collapse effects of uncontrolled, high rate UDP traffic loads. In other words, since UDP senders cannot detect congestion, network-based elements such as routers using packet queueing and dropping techniques will often be the only tool available to slow down excessive UDP traffic. The
Datagram Congestion Control Protocol (DCCP) is being designed as a partial solution to this potential problem by adding end host TCP-friendly congestion control behavior to high-rate UDP streams such as streaming media.
While the total amount of UDP traffic found on a typical network is often on the order of only a few percent, numerous key applications use UDP, including the
Domain Name System (DNS), the
simple network management protocol (SNMP), the
Dynamic Host Configuration Protocol (DHCP) and the
Routing Information Protocol (RIP), to name just a few.
The following, minimalistic example shows how to use UDP for client/server communication under
Unix:
Common code for server and client:
#include #include #include #include #include #include #include
#define BUFLEN 512#define PORT 9930
void diep(char *s){ perror(s);
exit(1);
}
The server:
int main(void){ struct sockaddr_in *sock_server, *sock_client;
int s;
socklen_t socklen=sizeof(struct sockaddr_in);
char buf[BUFLEN];
sock_server = malloc( socklen ); sock_client = malloc( socklen );
if ( !sock_server ¦¦ !sock_client ) diep( "allocation failed" );
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
sock_server->sin_family = AF_INET; sock_server->sin_port = htons(PORT); sock_server->sin_addr.s_addr = htonl(INADDR_ANY); if ( bind(s, (struct sockaddr *)sock_server, socklen )==-1)
diep("bind");
while( 1 ) { if (recvfrom(s, buf, BUFLEN, 0,
(struct sockaddr *)sock_client, &socklen)==-1)
diep("recvfrom()");
printf("Received packet from %s:%d\nData: %s\n",
inet_ntoa(sock_client->sin_addr), ntohs(sock_client->sin_port), buf); }
close(s); return 0;
}The client (replace 127.0.0.1 by the ip address of the server):
int main(void){ struct sockaddr_in *sock_server;
int s, i;
socklen_t socklen=sizeof(struct sockaddr_in);
char buf[BUFLEN];
sock_server = malloc( socklen ); if ( !sock_server )
diep( "allocation failed" );
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
sock_server->sin_family = AF_INET; sock_server->sin_port = htons(PORT); if (inet_aton("127.0.0.1", &(sock_server->sin_addr))==0) { fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
for (i=0; i<3; i++) { printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, BUFLEN, 0,
(struct sockaddr*)sock_server, socklen)==-1)
diep("sendto()"); }
close(s); return 0;
}Scripting languages provide compact wrappers for socket specific system calls. A server which is basically equivalent to the above C example requires only a few lines in
Ruby:
#!/usr/bin/rubyrequire 'socket'
serv = UDPSocket.openserv.bind('',9930) # listen at port 9930 to connections from any IP addresswhile true do p serv.recvfrom(512) # receive block of fixed length 512
end
WCF performs network programming at a much higher level than
Sockets. WCF implements UDP as a one-way channel, Support for a return address feature in an inherently one-way transport channel is provided by the CompositeDuplex channel
UDP does not provide reliability features. WCF supports the WS-ReliableMessaging protocol. WS-ReliableMessaging exposes three different guarantees: AtMostOnce, AtLeastOnce, and InOrder.
*
TCP and UDP port numbers for a complete (growing) list of ports/services
*
Connectionless protocol*
UDP flood attack*
UDP Data Transport*
Reliable User Datagram Protocol (RUDP)
*
Transmission Control Protocol*RFC 768
*
IANA Port Assignments*
The Trouble with UDP Scanning (PDF)*
Breakdown of UDP frame*
UDP on MSDN Magazine Sockets and WCF