UDP User Datagram Protocol
Jan 20, 2016
UDP
User Datagram Protocol
About the UDP A commonly used transport protocol
Does not guarantee either packet delivery or order
The packets may travel along different paths
Each packet has a time-to-live (TTL) counter
Arrives intact or is idscarded
The advantages of UDP More efficient than guaranteed-delivery
Unlike TCP streams, which establish a connection
Real-time application that demand up-to-second or better performance may be candidates for UDP
can receive data from more than one host machine
Some network protocols specify UDP as the transport mechanism (e.g. …)
Java classes to support UDP
java.net.DatagramPacket
java.net.DatagramSocket
DatagramPacket class Packets are containers for a small seq
uence of bytes, and include addressing information such as an IP address and a port number
The DatagramPacket class repesents a data packet intended for transmission using the UDP protocol
Note: When a DatagramPacket is used to send an
UDP packet, the IP address stored in DatagramPacket represents the address of the recipient (likewise with the port number)
When a DatagramPacket has been read from a UDP socket, the IP address of the packet represents the address of the sender (likewise with the port number)
Creating a DatagramPacket
To send data to a remote machine using UDP
To receive data sent by a remote machine using UDP
Constructors For receiving incoming UDP packets:
DatagramPacket(byte[] buffer, int length) For example:
DatagramPacket packet=new DatagramPacket(new byte[256], 256);
To send a DatagramPacket to a remote machine: DatagramPacket(byte[] buffer, int length, InetAddress dest_
addr, int dest_port) For example:
InetAddress addr=InetAddress.getByName(“192.168.0.1”)DatagramPacket packet=new DatagramPacket(new byte[128], 1
28, addr, 2000);
Using a DatagramPacket Methods
InetAddress getAddress() byte[] getData() int getLength() int getPort()
void setAddress(InetAddress addr) void setData(byte[] buffer) void setLength(int length) void setPort(int port)
DatagramSocket class
Provides access to a UDP socket, which allow UDP packets to be sent and received
Creating a DatagramSocket Constructors
To create a client DatagramSocket: DatagramSocket()
To create a server DatagramSocket: DatagramSocket(int port)
Using a DatagramSocket Methods
void close()
InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort()
void receive(DatagramPacket packet)
Listening for UDP packetsDatagramSocket socket=new DatagramSocket(2000);DatagramPacket packet=new DatagramPacket(new byte[2
56], 256);
While (! finished){
socket.receive(packet); //read operations are blocking// process the packet
}socket.close();
Processing UDP packets
ByteArrayInputStream bin=new ByteArrayInputStream(packet.getData());
DataInputStream din=new DataInputStrteam(bin);
// Read the contents of the UDP packet……
Sending UDP packetsDatagramSocket socket=new DatagramSocket(2005);DatagramPacket packet=new DatagramPacket(new byte[25
6], 256);packet.setAddress(InetAddress.getByName(somehost));packet.setPort(2000);Boolean finished=false;While (! finished){
// write data to packet buffer……socket.send(packet);
}
Code for PacketReceiveDemo……
Code for PacketSendDemo……