udp

22
UDP User Datagram Protocol

Upload: ulema

Post on 20-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UDP

UDP

User Datagram Protocol

Page 2: UDP

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

Page 3: UDP
Page 4: UDP

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

Page 5: UDP

Java classes to support UDP

java.net.DatagramPacket

java.net.DatagramSocket

Page 6: UDP

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

Page 7: UDP
Page 8: UDP

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)

Page 9: UDP

Creating a DatagramPacket

To send data to a remote machine using UDP

To receive data sent by a remote machine using UDP

Page 10: 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);

Page 11: UDP

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)

Page 12: UDP

DatagramSocket class

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

Page 13: UDP

Creating a DatagramSocket Constructors

To create a client DatagramSocket: DatagramSocket()

To create a server DatagramSocket: DatagramSocket(int port)

Page 14: UDP

Using a DatagramSocket Methods

void close()

InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort()

void receive(DatagramPacket packet)

Page 15: UDP

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

Page 16: UDP
Page 17: UDP

Processing UDP packets

ByteArrayInputStream bin=new ByteArrayInputStream(packet.getData());

DataInputStream din=new DataInputStrteam(bin);

// Read the contents of the UDP packet……

Page 18: UDP
Page 19: UDP

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

}

Page 20: UDP
Page 21: UDP

Code for PacketReceiveDemo……

Page 22: UDP

Code for PacketSendDemo……