Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming with UDP

Similar presentations


Presentation on theme: "Socket Programming with UDP"— Presentation transcript:

1 Socket Programming with UDP

2 What is UDP? User Datagram Protocol UDP is
“Unreliable” Datagram Protocol? UDP is unreliable no guarantee on delivery sequence no connection (less overhead)‏

3 When would we use UDP? Quick short bursts of communication
Connection overhead would be inefficient e.g. DNS queries Real time applications No congestion control, sending rate unregulated e.g. audio/video streaming, loss of some packets is not critical No connection state needed More connections can be handled at endpoint

4 UDP – Packet Format 2 bytes for source/destination ports (0-65536)‏
MAC Header IP Header UDP Header Data Data Packet 32 bits Source Port Dest Port Length Checksum Data 2 bytes for source/destination ports ( )‏ Length (bytes) = header + data Checksum of header and data Data = variable length multiple of 4 bytes, padding done by kernel

5 Socket programming with UDP
UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server CPSC Application Layer

6 UDP Sockets in C CPSC Application Layer

7 Socket programming with UDP
Socket creation with SOCK_DGRAM int s = socket(PF_INET, SOCK_DGRAM,0); No explicit connection No listen()‏ No accept()‏

8 Client-Server Flow Client Server Connectionless!
No listen(), accept(), or connect()* calls socket() socket() sendto() bind() recvfrom() wait for next packet from arbitrary client sendto() recvfrom() close() close()

9 UDP socket methods - sendto()‏
ssize_t sendto( int sockfd,void *buff, size_t nbytes, int flags, const struct sockaddr* to, socklen_t addrlen); sendto() instead of send(); must specify client to send packet to Returns bytes sent to the kernel network stack, not what was sent to the client! Possible to send 0 bytes of data! No error message to indicate non-receipt of data Possible errors: EBADF, ENOTSOCK: bad socket descriptor EFAULT: bad buffer address EMSGSIZE: message too large ENOBUFS: system buffers are full

10 UDP socket methods - recvfrom()‏
ssize_t recvfrom( int sockfd, void *buff, size_t nbytes,int flags, struct sockaddr* from, socklen_t *fromaddrlen); recvfrom() instead of recv(); can receive from anyone Buffer must be large enough, else data is lost forever recvfrom() is blocking, returns only on packet receipt Same errors as sendto, with addition of EINTR: System call interrupted by signal Useful if recvfrom() should timeout after a while

11 Issues with UDP packets
Datagram size Should not be too large, else IP fragmentation occurs increases the chance of lost packets Connected UDP sockets Calling connect on a socket specifies the destination Can use send()/recv() instead of sendto()/recvfrom If server calls connect, will not receive packets from anyone else ICMP error messages If server is not listening on port, it returns and ICMP error message to the sending host sendto() may not return this error until next call to socket! If ICMP error received, sendto() returns ECONNREFUSED

12 UDP Sockets in Java CPSC Application Layer

13 Example: Java client (UDP)
process Input: receives packet (recall thatTCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket CPSC Application Layer

14 Example: Java client (UDP)
import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS CPSC Application Layer

15 Example: Java client (UDP), cont.
Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Send datagram to server Read datagram from server CPSC Application Layer

16 Example: Java server (UDP)
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram CPSC Application Layer

17 Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram CPSC Application Layer


Download ppt "Socket Programming with UDP"

Similar presentations


Ads by Google