Title: UDP: User Datagram Protocol
1UDP User Datagram Protocol
2UDP User Datagram Protocol RFC 768
- Pros
- No connection establishment
- No delay to start sending/receiving packets
- Simple
- no connection state at sender, receiver
- Small segment header
- Just 8 bytes of header
- Cons
- best effort transport service means, UDP
segments may be - lost
- delivered out of order to app
- no congestion control UDP can blast away as fast
as desired
- bare bones, best effort transport protocol
- connectionless
- no handshaking between UDP sender, receiver
before packets start being exchanged - each UDP segment handled independently of others
- Just provides multiplexing/demultiplexing
3UDP more
Used for Mux/Demux
- often used for streaming multimedia apps
- loss tolerant
- rate sensitive
- other UDP uses
- DNS
- SNMP
- reliable transfer over UDP add reliability at
application layer - application-specific error recovery!
32 bits
source port
dest port
Length, in bytes of UDP segment, including header
checksum
length
Application data (message)
UDP segment format
4UDP Demultiplexing
- Create sockets with port numbers
- DatagramSocket mySocket1 new DatagramSocket(6428
) - DatagramSocket mySocket2 new DatagramSocket(4567
) - UDP socket identified by two-tuple
- (dest IP address, dest port number)
- When host receives UDP segment
- checks destination port number in segment
- directs UDP segment to socket with that port
number - IP datagrams with different source IP addresses
and/or source port numbers directed to same socket
5UDP Demultiplexing Example
- DatagramSocket serverSocket new
DatagramSocket(6428)
SP 9157
client IP A
Client IPB
server IP C
DP 6428
Source Port (SP) provides return address
Identifies the process at the other end of the
line
6UDP checksum
Goal detect errors (e.g., flipped bits) in
transmitted segment
- Receiver
- compute checksum of received segment
- check if computed checksum equals checksum field
value - NO - error detected
- YES - no error detected. But maybe errors
nonetheless? - Reordered bytes
- Why checksum at UDP if LL provides error
checking? - IP is supposed to run over ANY LL, so UDP does
its own error checking
- Sender
- treat segment contents as sequence of 16-bit
integers - checksum addition (1s complement sum) of
segment contents - sender puts checksum value into UDP checksum field
7How to program using the UDP?
- Socket Layer
- Programmers API to the protocol stack
- Typical network app has two pieces client and
server - Server Passive entity. Provides service to
clients - e.g., Web server responds with the requested Web
page - Client initiates contact with server (speaks
first) - typically requests service from server, e.g., Web
Browser
8Socket Creation
- mySock socket(family, type, protocol)
- UDP/TCP/IP-specific sockets
- Socket reference
- File (socket) descriptor in UNIX
- Socket handle in WinSock
9UDP Client/Server Interaction
Server starts by getting ready to receive
client messages
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
10UDP Client/Server Interaction
/ Create socket for incoming messages /
if ((servSock socket(PF_INET, SOCK_DGRAM,
IPPROTO_UDP)) lt 0) Error("socket()
failed")
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
11UDP Client/Server Interaction
ServAddr.sin_family PF_INET
/ Internet address family /
ServAddr.sin_addr.s_addr htonl(INADDR_ANY)
/ Any incoming interface /
ServAddr.sin_port htons(20000)
/ Local port 20000 / if
(bind(servSock, (struct sockaddr ) ServAddr,
sizeof(ServAddr)) lt 0) Error("bind()
failed")
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
12Specifying Addresses
- struct sockaddr
-
- unsigned short sa_family / Address family
(e.g., PF_INET) / - char sa_data14 /
Protocol-specific address information / -
- struct sockaddr_in
-
- unsigned short sin_family / Internet
protocol (PF_INET) / - unsigned short sin_port / Port
(16-bits) / - struct in_addr sin_addr / Internet
address (32-bits) / - char sin_zero8 / Not used /
-
- struct in_addr
-
- unsigned long s_addr / Internet
address (32-bits) / -
Generic
IP Specific
13UDP Client/Server Interaction
struct sockaddr_in peer
int peerSize sizeof(peer) char
buffer65536 recvfrom(servSock, buffer,
65536, 0, (struct sockaddr )peer, peerSize)
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
14UDP Client/Server Interaction
Server is now blocked waiting for a message from
a client
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
15UDP Client/Server Interaction
Later, a client decides to talk to the server
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
16UDP Client/Server Interaction
/ Create socket for outgoing messages /
if ((clientSock socket(PF_INET, SOCK_DGRAM,
IPPROTO_UDP)) lt 0) Error("socket()
failed")
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
17UDP Client/Server Interaction
// Initialize servers address and port
struct sockaddr_in server server.sin_family
AF_INET server.sin_addr.s_addr
inet_addr(10.10.100.37) server.sin_port
htons(20000) // Send it to the server
sendto(clientSock, buffer, msgSize, 0, (struct
sockaddr )server, sizeof(server))
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket
18UDP Client/Server Interaction
close(clientSock)
close(serverSock)
- Server
- Create a UDP socket
- Assign a port to socket
- Communicate (receive/send messages)
- When done, close the socket
- Client
- Create a UDP socket
- Communicate (send/receive messages)
- When done, close the socket