Presentation is loading. Please wait.

Presentation is loading. Please wait.

UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.

Similar presentations


Presentation on theme: "UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel."— Presentation transcript:

1 UDP User Datagram Protocol

2 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

3

4 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. … )

5 Java classes to support UDP java.net.DatagramPacket java.net.DatagramSocket

6 DatagramPacket class Packets are containers for a small sequence 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

7

8 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)

9 Creating a DatagramPacket To send data to a remote machine using UDP To receive data sent by a remote machine using UDP

10 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], 128, addr, 2000);

11 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)

12 DatagramSocket class Provides access to a UDP socket, which allow UDP packets to be sent and received

13 Creating a DatagramSocket Constructors To create a client DatagramSocket: DatagramSocket() To create a server DatagramSocket: DatagramSocket(int port)

14 Using a DatagramSocket Methods void close() InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort() void receive(DatagramPacket packet)

15 Listening for UDP packets DatagramSocket socket=new DatagramSocket(2000); DatagramPacket packet=new DatagramPacket(new byte[256], 256); While (! finished) { socket.receive(packet);//read operations are blocking // process the packet } socket.close();

16

17 Processing UDP packets ByteArrayInputStream bin=new ByteArrayInputStream(packet.getData()); DataInputStream din=new DataInputStrteam(bin); // Read the contents of the UDP packet ……

18

19 Sending UDP packets DatagramSocket socket=new DatagramSocket(2005); 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); }

20

21 Code for PacketReceiveDemo ……

22 Code for PacketSendDemo ……


Download ppt "UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel."

Similar presentations


Ads by Google