UDP User Datagram Protocol About the UDP n
UDP User Datagram Protocol
About the UDP n n A commonly used transport protocol Does not guarantee either packet delivery or order n The packets may travel along different paths n Each packet has a time-to-live (TTL) counter n Arrives intact or is idscarded
The advantages of UDP n More efficient than guaranteed-delivery n Unlike TCP streams, which establish a connection n 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 n java. net. Datagram. Packet n java. net. Datagram. Socket
Datagram. Packet class n n Packets are containers for a small sequence of bytes, and include addressing information such as an IP address and a port number The Datagram. Packet class repesents a data packet intended for transmission using the UDP protocol
Note: n n When a Datagram. Packet is used to send an UDP packet, the IP address stored in Datagram. Packet represents the address of the recipient (likewise with the port number) When a Datagram. Packet 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 Datagram. Packet n n To send data to a remote machine using UDP To receive data sent by a remote machine using UDP
n Constructors n For receiving incoming UDP packets: n n Datagram. Packet(byte[] buffer, int length) For example: Datagram. Packet packet=new Datagram. Packet(new byte[256], 256); n To send a Datagram. Packet to a remote machine: n n Datagram. Packet(byte[] buffer, int length, Inet. Address dest_addr, int dest_port) For example: Inet. Address addr=Inet. Address. get. By. Name(“ 192. 168. 0. 1”) Datagram. Packet packet=new Datagram. Packet(new byte[128], 128, addr, 2000);
Using a Datagram. Packet n Methods n n n n Inet. Address get. Address() byte[] get. Data() int get. Length() int get. Port() void set. Address(Inet. Address addr) set. Data(byte[] buffer) set. Length(int length) set. Port(int port)
Datagram. Socket class n Provides access to a UDP socket, which allow UDP packets to be sent and received
Creating a Datagram. Socket n Constructors n To create a client Datagram. Socket: n n Datagram. Socket() To create a server Datagram. Socket: n Datagram. Socket(int port)
Using a Datagram. Socket n Methods n void close() n Inet. Address get. Inet. Address() int get. Port() Inet. Address get. Local. Address() int get. Local. Port() n void receive(Datagram. Packet packet) n n n
Listening for UDP packets Datagram. Socket socket=new Datagram. Socket(2000); Datagram. Packet packet=new Datagram. Packet(new byte[256], 256); While (! finished) { socket. receive(packet); //read operations are blocking // process the packet } socket. close();
Processing UDP packets Byte. Array. Input. Stream bin=new Byte. Array. Input. Stream(packet. get. Data()); Data. Input. Stream din=new Data. Input. Strteam(bin); // Read the contents of the UDP packet ……
Sending UDP packets Datagram. Socket socket=new Datagram. Socket(2005); Datagram. Packet packet=new Datagram. Packet(new byte[256], 256); packet. set. Address(Inet. Address. get. By. Name(somehost)); packet. set. Port(2000); Boolean finished=false; While (! finished) { // write data to packet buffer …… socket. send(packet); }
Code for Packet. Receive. Demo ……
Code for Packet. Send. Demo ……
- Slides: 22