User Datagram Protocol

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

User Datagram Protocol : 

User Datagram Protocol

Overview : 

Overview UDP is a commonly used transport protocol employed by many types of applications A connectionless transport – doesn’t guarantee either packet delivery or packets arrive in sequential order Bytes are grouped together in discrete packets not an ordered sequence of bytes using I/O stream

Cont. : 

Cont. The packet may travel along different path as selected by the various network routers that distribute traffic flow (network congestion, priority of routes and the cost of transmission) Possibility of packet missing or discarded exist No notification is available to update the status of delivery

UDP Advantages : 

UDP Advantages UDP communication can be more efficient than guaranteed-delivery data streams. If the amount is small and the data is sent frequently, it may make sense to avoid the overhead of guaranteed delivery Overhead to establish connection can be reduced as compared to TCP Real time applications that demand up-to-the-second or better performance maybe the candidates for UDP, since fewer delay due to the error checking and flow control of TCP

Cont. : 

Cont. UDP sockets can receive data from more than one host machine Some network protocols specify UDP as the transport mechanism requiring its use Java supports the UDP in the form of two classes: Java.net.DatagramPacket Java.net.DatagramSocket

DatagramPacket Class : 

DatagramPacket Class Represents a data packet intended for transmission using the User Datagram Protocol Packets are containers for a small sequence of bytes and include addressing information such as IP address and a port

DatagramPacket Representation : 

DatagramPacket Representation DatagramPacket IP Address (java.net.InetAddr) Port address Packet data Byte[ ] = {…,…,…,…,…,..,}

Cont. : 

Cont. The meaning of the data stored in a DatagramPacket is determined by its context Incoming packet – IP address (sender) Outgoing packet – IP address (recipient)

Creating a DatagramPacket : 

Creating a DatagramPacket Two reasons to create a new DatagramPacket To send data to a remote machine using UDP To receive data sent by a remote machine using UDP

Constructors : 

Constructors The choice of which constructor to use is determined by its intended purpose Constructor requires the specification of a byte array used to store the UDP packet contents and the length of the data packet

Receiving packet : 

Receiving packet Constructor to be used: DatagramPacket(byte[] buffer, int length) For example: DatagramPacket packet = new DatagramPacket( new byte[256], 356);

Sending Packet : 

Sending Packet Constructor to be used: DatagramPacket(byte[ ] buffer, int length, InetAddress dest_addr, int dest_port) For example: InetAddress addr = InetAddress.getByName(“Hostname”); DatagramPacket packet = new DatagramPacket (new byte[128], 128, addr, 2000);

Using a DatagramPacket : 

Using a DatagramPacket The DatagramPacket class provides some important methods that allow the remote address, remote port, data (as a byte array) and the length of the packet to be retrieved Methods: InetAddress getAddress( ) – returns the IP address from which a DatagramPacket was sent or the destination IP address byte[] getData( ) – returns the contents of the DatagramPacket represented as an array of bytes

Cont. : 

Cont. int getLength( ) – returns the length of the data stored in a DatagramPacket int getPort( ) – returns the port number from which a DatagramPacket was sent or the destination port number void SetAddress(InetAddress addr) – assigns a new destination address to a DatagramPacket void setData(byte[ ] buffer) – assigns a new data buffer to the DatagramPacket void setLength(int length) – assigns a new length to the DatagramPacket void setPort(int port) – assigns a new destination port to a DatagramPacket

DatagramSocket Class : 

DatagramSocket Class Provides access to a UDP socket that allows UDP packets to be sent and received A DatagramPacket is used to represent a UDP packet and must be created prior to receiving any packets The same DatagramSocket can be used to receive packets as well as to send them.

Cont. : 

Cont. Read operations are blocking that means the application will continue to wait until a packet arrives Can cause an application to stall of the sender resubmit packets in case error occurs during transmission Use multiple threads of execution or nonblocking IO operation

Creating a DatagramSocket : 

Creating a DatagramSocket Can be used to send and receive packets Each DatagramSocket binds to a port on the local machine which is used for addressing packets Important for a server application not for client

Constructors : 

Constructors To create a client DatagramSocket, the constructor to be used: DatagramSocket( ) throws java.net.SocketException To create a server DatagramSocket, the constructor to be used: DatagramSocket( int port) – takes the port number to be bound by the UDP service

Using a DatagramSocket : 

Using a DatagramSocket Is used to receive incoming and to send outgoing UDP packets Provides methods to send and to receive packets as well as tp specify a timeout value when nonblocking IO is being used, to inspect and modify maximum UDP packet sizes and to close the socket

Methods : 

Methods void close ( ) – closes the socket and unbinds it from the local port void connect(InetAddress remote_addr, int remote_port) – restricts access to the specified remote address and port void disconnect( ) – disconnects the DatagramSocket and removes any restrictions imposed on it by an earlier connect operation InetAddress getInetAddress( ) – returns the local address to which the socket is connected or null if no such connection exists

Cont. : 

Cont. int getPort( ) – returns the remote port to which the socket is connected or -1 if no such connection exists InetAddress getLocalAddress( ) – returns the local address to which the socket is bound int getLocalPort( ) – returns the local port to which the socket is bound int getReceiveBufferSize( ) – returns the maximum buffer size used for incoming UDP packets int getSendBufferSize( ) – returns the maximum buffer size used for outgoing UDP packets

Cont. : 

Cont. Int getSoTimeout( ) – returns the value of the timeout socket option. This value is used to determine the number of miliseconds a read operation will block before throwing an exception. By default, the value will be 0 indicating that blocking IO will be used Void receive(DatagramPacket packet) – read a UDP packet and stores the contents in the specified packet Void send(DatagramPacket packet) – sends a UDP packet represented by the specified packet parameter

Cont. : 

Cont. Void setReceiveBufferSize(int length) – sets the maximum buffer size used for incoming UDP packets Void setSendBufferSize(int length) – sets the maximum buffer size used for outgoing UDP packets Void setSoTimeout(int duration) – sets the value of the timeout socket option

Listening for UDP Packets : 

Listening for UDP Packets Before an application can read UDP packets sent to it by remote machines, it must bind a socket to a local UDP port using DatagramSocket and create a DatagramPacket that will act as a container for the UDP packet’s data When an application wishes to read UDP packets, it calls the DatagramSocket.receive method which copies a UDP packet into the specified DatagramPacket. The contents of the DatagramPacket are processed and the process is repeated as needed

Example : 

Example DatagramPacket packet = new DatagramPacket(new byte[256], 256); DatagramSocket = new DatagramSocket(2000); boolean finished = false; while (!finished) { socket.receive(packet); //process the packet } Socket.close(): Packet DatagramSocket DatagramPacket UDP Application Translates packet into a datagramPacket Reads packets

Connect to Streams : 

Connect to Streams Many developers prefers to use Java I/O streams to process data, using DatInputStream or a BufferedReader to access the contents of byte arrays For example: ByteArrayInputStream bin = new ByteArrayInputStream (packet.getData() ); DataInputStream din = new DataInputStream (bin); //Read the contents of the UDP packet

Sending UDP packets : 

Sending UDP packets Using the same interface (DatagramSocket) to receive and send packet When sending a packet, the application must create a datagramPacket, set the address and port information and write the data intended for transmission to its byte array If replying to a received packet, the address and port information will already be stored and only the data need to be overwritten Once the packet is ready, the send method will be invoked

Sending packets : 

Sending packets UDP application DatagramSocket DatagramPacket packet Binds to a UDP port Constructs packet Send DatagramPacket Using DatagramSocket

Code Snippet : 

Code Snippet DatagramSocket socket = new DatagramSocket (2000); DatagramPacket packet = new DatagramPacket(new byte[256], 256); Packet.setAddress(InetAddress.getByName(Somehost) ); Packet.setPort(2000); Boolean finished = false; While ( !finished ) { //write data to packet buffer ….. socket.send (packet); //Do something else, like read other packets etc ….. } Socket.close ( );