UDP client-server: Introduction : 1) It stands for user datagram protocol sometime universal datagram protocol. 2) 20% part of overall internet traffic uses udp. 3) Both socket(Client+server) have to make their existence known to the system using “bind”. 4) For each message client has to find server. 5) Bind uses new port, if currently using port no has given then error occurs so that while binding we use ZERO as port number so it takes unused port. 6) Instead of read/write we use recvfrom() / sendto(), which takes parameter the socket to write to & address of service on the remote machine. SERVER
CLIENT
Socket()
Socket()
Bind()
Bind()
Recvfrom()
Sendto()
Sendto()
Recvfrom()
SERVER : Create endpoint (socket()) bind address (bind()) transfer data (sendto() recvfrom()) CLIENT : Create endpoint(socket()) bind address (bind)) connect to server (connect()) transfer data (sendto() recvfrom())
1|Page Created By : Kishor (
[email protected])
What Is UDP? UDP is a minimal message-oriented transport layer that uses ports to provide packet-switched application-to-application communication over a network. UDP encapsulates messages – or packets – in the form of a datagram (see Figure 2).
Figure 2. A Typical UDP Datagram A datagram is logically arranged in two parts – the datagram header and message. Each datagram header is further broken down into four sections, as illustrated in Figure 2. The minimum length of a single UDP datagram is the size of the datagram header, or 8 bytes. UDP is characterized by this minimal overhead and small packet size, which translates to higher throughput when compared to TCP. However, because inherent error checking and the guarantee of data receipt are sacrificed to achieve increased throughput, TCP becomes the more beneficial choice in applications where large message size or transmission reliability is important.
When to Use UDP
Benefits
Use Cases
5X smaller packet sizes than TCP
Large amount of data transfer with acceptable data loss
Less network traffic
Fast data streaming
Broadcast and multicast support
Reduction of network connections
2|Page Created By : Kishor (
[email protected])
TCP Socket versus UDP Socket Transmission Control Protocaol Socket Client-Server architecture Connection-oriented with error control
User Datagram Protocol Socket Peer-to-Peer architecture Connectionless without error control
3|Page Created By : Kishor (
[email protected])
Creating a UDP Datagram Socket Application A UDP server does not have to listen for and accept client connections, and a UDP client does not have to connect to a server. The following illustration shows the interaction between the UDP server and UDP client.
To create a UDP datagram socket server application
1. Open a datagram socket with socket. Use AF_INET for the address format parameter and SOCK_DGRAM for the type parameter. To prepare for association with a client, a UDP datagram server need only create a socket and bind it to a name when preparing for association with a client. 2. Name the socket with the bind function, using a SOCKADDR_IN structure for the address parameter. 3. Exchange data with a client using the sendto and recvfrom functions. The UDP datagram socket server application calls recvfrom to prepare to receive data from a client Therecvfrom function reads incoming data on unconnected sockets and captures the address from which the data was sent. To do this, the local address of the socket must be known. The sendto function is used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Successfully completing a sendto function call does not confirm data was successfully delivered. 4. Close the connection with the closesocket function. Calling the shutdown function is unnecessary for UDP sockets. To create a UDP datagram socket client application
1. Open a socket with the socket function. 2. Exchange data with server using sendto and recvfrom. A UDP datagram client socket is named when the client calls sendto. 3. Close the connection with the closesocket functions. Calling shutdown is unnecessary for UDP sockets.
4|Page Created By : Kishor (
[email protected])