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