1 / 22

UDP

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.

ulema
Télécharger la présentation

UDP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  4. Java classes to support UDP • java.net.DatagramPacket • java.net.DatagramSocket

  5. 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

  6. 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)

  7. Creating a DatagramPacket • To send data to a remote machine using UDP • To receive data sent by a remote machine using UDP

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

  9. 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)

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

  11. Creating a DatagramSocket • Constructors • To create a client DatagramSocket: • DatagramSocket() • To create a server DatagramSocket: • DatagramSocket(int port)

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

  13. 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();

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

  15. 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); }

  16. Code for PacketReceiveDemo ……

  17. Code for PacketSendDemo ……

More Related