user datagram protocol. introduction udp is a connectionless transport protocol, i.e. it...

36
User Datagram Protocol

Post on 20-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

User Datagram Protocol

Page 2: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Introduction

UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive in sequential order.

With UDP, bytes of data are grouped together in discrete packets which are sent over the network.

Page 3: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Packets may travel along different paths depending on the state of the network.

No two packets are guaranteed the same route.

Each packet has a time-to-live (TTL) counter, which is updated when it is routed along to the next point in the network. When the timer expires, it will be discarded, and the recipient will not be notified.

Page 4: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

If a packet does arrive, it will always arrive intact. Packets that are corrupt or only partially delivered are discarded.

Page 5: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Advantages of UDP

UDP communication can be more efficient than guaranteed-delivery data streams.

Unlike TCP streams, which establish a connection, UDP causes fewer overheads.

Real-time applications that demand up-to-the-second or better performance may be candidates for UDP, as there are fewer delays due to error checking and flow control of TCP.

Page 6: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

UDP sockets can receive data from more than one host machine.

Some network protocols specify UDP as the transport mechanism.

Page 7: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Java Support for UDP

Two classes are provided:DatagramPacket class (java.net)DatagramSocket class (java.net)

Page 8: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramPacket Class

A DatagramPacket object represents a data packet intended for transmission using UDP.

It contains addressing information such as an IP address and a port.

When a DatagramPacket is read from a UDP socket, the IP address/port of the packet represents the address/port of the sender.

Page 9: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

When a DatagramPacket is used to send a UDP packet, the IP address/port represents the address/port of the recipient.

DatagramPacket

IP address (java.net.InetAddr)

Port address (int)

Packet data (byte[])

Page 10: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Creating a DatagramPacket Constructor to use for creating a DatagramPacket for receiving incoming UDP packets:DatagramPacket(byte[] buffer, int length)

Example:DatagramPacket packet;packet = new DatagramPacket(new byte[256], 256);

Page 11: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Constructor to use for sending a DatagramPacket to a remote machineDatagramPacket(byte[] buffer, int length,

InetAddress dest_addr, int dest_port)

Example:DatagramPacket packet;

InetAddress addr;

addr = InetAddress.getByName("192.168.0.1");

packet = new DatagramPacket(new byte[128], 128,addr,

2000);

Page 12: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramPacket Methods

Refer p121. 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 13: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramSocket Class

The DatagramSocket class provides access to a UDP socket, which allows UDP packets to be sent and received.

The same DatagramSocket can be used to receive as well as to send packets.

read operations are blocking - i.e. the application will continue to wait until a packet arrives.

Page 14: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Each DatagramSocket binds to a port on the local machine. The port number need not match the port number of the remote machine.

If the application is a UDP server, it will usually bind to a specific port number.

Page 15: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Creating a DatagramSocket Constructor to use for creating a client DatagramSocket:DatagramSocket() throws java.net.SocketException

Example:DatagramSocket socket;

try {socket = new DatagramSocket();

} catch (SocketException exception) {

}

Page 16: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Constructor to use for creating a server DatagramSocket:DatagramSocket(int port) throws

java.net.SocketException

Example:DatagramSocket socket;

try {socket = new DatagramSocket(2000);

} catch (SocketException exception) {

}

Page 17: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramSocket Methods Refer p123.

void close()void connect(InetAddress r_addr, int r_port)void disconnect() InetAddress getInetAddress() int getPort() InetAddress getLocalAddress() int getLocalPort() int getReceiveBufferSize() throws java.net.SocketException

Page 18: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

int getSendBufferSize() throws java.net.SocketException

int getSoTimeout() throws java.net.SocketExceptionvoid receive(DatagramPacket packet) throws

java.io.IOExceptionvoid send(DatagramPacket packet) throws

java.io.IOException int setReceiveBufferSize(int length) throws

java.net.SocketException int setSendBufferSize(int length) throws

java.net.SocketExceptionvoid setSoTimeout(int duration) throws

java.net.SocketException

Page 19: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Listening for UDP Packets

Before an application can read UDP packets, it must bind a socket to a local UDP port using DatagramSocket

create a DatagramPacket that will contain the data.

Page 20: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Packet

DatagramSocket DatagramPacket

UDP application

Reads packets

Translates packetsInto a DatagramPacket

Page 21: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

The following code illustrates the process for reading UDP packets.

DatagramPacket packet;DatagramSocket socket;packet = new DatagramPacket(new byte[256], 256);socket = new DatagramSocket(2000);boolean finished = false;while (!finished) {

socket.receive(packet);// process the packet

}socket.close();

Page 22: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Java I/O streams are usually used to access the contents of the byte array in a DatagramPacket. See example later.

DatagramPacket

IP address (java.net.InetAddr)

Port address (int)

Packet data (byte[])

ByteArrayInputStream

DataInputStream

UDPapplication

Page 23: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Sending UDP Packets

When sending a packet, the application must create a DatagramPacket that will contain the data. The address and port information must also be set.

When the packet is ready for transmission, the send method of DatagramSocket should be invoked.

Page 24: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Packet

DatagramSocket

DatagramPacket

UDP application

Binds to a UDP port

Constructspacket

Send DatagramPacketusing DatagramSocket

Page 25: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

The following code illustrates the process for sending UDP packets.

DatagramPacket packet;DatagramSocket socket;packet = new DatagramPacket(new byte[256], 256);socket = new DatagramSocket(2000);packet.setAddress(…);packet.setPort(2000);boolean finished = false;while (!finished) {

// write data to packet buffersocket.send(packet);…

}socket.close();

Page 26: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

User Datagram Protocol Example

Run receiving application

java PacketReceiveDemo Run sending application

java PacketSendDemo

Page 27: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramSocket

PacketSendDemo

ByteArrayOutputStream

PrintStreamDatagramPacket

print(str)

toByteArray()

send(packet)

Page 28: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

DatagramSocket

PacketReceiveDemo

ByteArrayInputStream

DatagramPacket

receive(packet)

read()

getData()

Page 29: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Building a UDP Client/Server

Run echo server

java EchoServer Run echo client

java EchoClient

Page 30: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Algorithm for Echo Server

1. Create socket

2. Create an empty packet

3. Repeat the following forever1. Wait for a packet

2. Send the packet back to sender

Page 31: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Algorithm for Echo Client1. Create socket2. Set timeout value for socket3. Repeat the following ten times

1. Create the message to be sent2. Create packet containing the message as well as the

destination IP and the port3. Send the packet through socket4. Wait for packet from receiver through socket or

timeout5. if packet received

1. Create an input stream to access data in the packet2. Use the input stream to read the data and then display it on

the screen.

6. Sleep for a second

Page 32: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Overcoming UDP Limitations

UDP limitations:Lack of Guaranteed DeliveryLack of Guaranteed Packet SequencingLack of Flow Control

Page 33: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Lack of Guaranteed Delivery

Packets sent via UDP may become lost in transit.

UDP packets can also become damaged or lost.

For some applications, the loss of individual packets may not have a noticeable effect (e.g. video streams).

For other applications, loss of packets is not acceptable (e.g. file transfers).

Page 34: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

If guaranteed delivery is required,avoid packet-based communication, and

use a more suitable transport mechanism (e.g. TCP).

send acknowledgement to sender after receiving packets.

Page 35: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Lack of Guaranteed Packet Sequencing Applications that require sequential access

to data should include a sequence number in the contents of a datagram packet.

This enables detection of duplicate packets and also missing packets.

Page 36: User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive

Lack of Flow Control The technique of flow control is important

to avoid flooding a system with more data than it can handle due to limited bandwidth.

One technique of flow control is to limit the number of unacknowledged packets. E.g.: increase control when number of acknowledgement packets received is much less than the number of packets sent.