Chapter 3 : User Datagram Protocol (UDP)

18
CHAPTER 3 : User Datagram Protocol (UDP) By : Mr. Asyran Zarizi Bin Abdullah

Transcript of Chapter 3 : User Datagram Protocol (UDP)

Page 1: Chapter 3 : User Datagram Protocol (UDP)

CHAPTER 3 : User Datagram Protocol (UDP)

By :

Mr. Asyran Zarizi Bin Abdullah

Page 2: Chapter 3 : User Datagram Protocol (UDP)

User Datagram Protocol

• It’s a connection-less oriented protocol

• Simpler and less overhead compared to TCP

• Two important classes provided by Java API under java.net

– DatagramSocket

• Class that support data transmission between sender and receiver

– DatagramPacket

• Class that support data packet formation

Page 3: Chapter 3 : User Datagram Protocol (UDP)

Layman Terms for UDP

Page 4: Chapter 3 : User Datagram Protocol (UDP)

Java.net API

• Socket in JAVA will divide by 3 type :

– Socket client for TCP (stream)

• Java.net.Socket

– Socket server for TCP (stream)

• Java.net.ServerSocket

– Socket server for UDP

• Java.net.DatagramSocket

Page 5: Chapter 3 : User Datagram Protocol (UDP)

java.net.DatagramSocket class

• Class that use to send and receive DatagramPacket from or to network

• Datagram is an information but no guarantee of its content, arrival queue and arrival time.

Constructor 1) DatagramSocket (int port)

2) DatagramSocket(int port, InetAddress inet)

3) DatagramSocket()

Sample Syntax

Page 6: Chapter 3 : User Datagram Protocol (UDP)

java.net.DatagramPacket class

• Class use to represent a packet information (array byte)

• Message that can be sent or received and can arrived in any order.

Constructor 1) DatagramPacket(byte[], buf, int length)

2) DatagramPacket(byte[] buf, int length, InetAddress address, int port)

Sample Syntax

Page 7: Chapter 3 : User Datagram Protocol (UDP)

Methods

• getData() : use to take an data information

• getLenght() : use to take datagram length

• getAddress() : use to take ip address information

• getPort() : use to take port address information

• Send(DatagramPacket data) : to send DatagramPacket to host and port destination

• Receive(DatagramPacket data) : to block execution until receive 1 complete packet

Page 8: Chapter 3 : User Datagram Protocol (UDP)

UDP Socket Structure

Did you remember About previous lab (MySend) And (MyReceiver)?

Page 9: Chapter 3 : User Datagram Protocol (UDP)

Example :

Page 10: Chapter 3 : User Datagram Protocol (UDP)

UDP vs TCP

User Datagram Protocol (UDP) Transmission Control Protocol (TCP)

Connectionless Oriented Connection-Oriented

Provide datagram services Provide stream services

No flow control, sequencing, and error recovery.

Handles flow control, sequencing, and error recovery.

Handles designation among processes in the same host by means of service port numbers.

Handles designation among processes in the same host by means of service port numbers.

There is a comparison between UDP and TCP

Page 11: Chapter 3 : User Datagram Protocol (UDP)

Datagram Socket SERVER 1. Create a DatagramSocket object

– DatagramSocket dgramSocket = new DatagramSocket(1234);

2. Create a buffer for incoming datagrams – byte[] buffer = new byte[256];

3. Create a DatagramPacket object for the incoming datagram

– DatagramPacket inPacket = new DatagramPacket(buffer,buffer.length);

4. Accept an incoming datagram – dgramSocket.receive(inPacket)

Page 12: Chapter 3 : User Datagram Protocol (UDP)

Datagram Socket SERVER 5. Accept the sender’s address and port from the packet

– InetAddress clientAddress = inPacket.getAddress(); – int clientPort = inPacket.getPort();

6. Retrieve the data from the buffer – string message = new String(inPacket.getData(), 0,

inPacket.getLength());

7. Create the response datagram – DatagramPacket outPacket = new

DatagramPacket(response.getBytes() , response.length(), clientAddress, clientPort);

8. Send the response datagram – dgramSocket.send(outPacket)

9. Close the DatagramSocket: dgram.close();

Page 13: Chapter 3 : User Datagram Protocol (UDP)

Datagram Packet CLIENT 1. Create a DatagramSocket object

– DatagramSocket dgramSocket = new DatagramSocket;

2. Create the outgoing datagram – DatagramPacket outPacket = new

DatagramPacket(message.getBytes(), message.length(),host, port);

3. Send the datagram message – dgramSocket.send(outPacket)

4. Create a buffer for incoming datagrams – byte[] buffer = new byte[256];

Page 14: Chapter 3 : User Datagram Protocol (UDP)

Datagram Packet CLIENT 5. Create a DatagramPacket object for the incoming datagram

– DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);

6. Accept an incoming datagram – dgramSocket.receive(inPacket)

7. Retrieve the data from the buffer – string response = new String(inPacket.getData(), 0,

inPacket.getLength());

8. Close the DatagramSocket: – dgram.close();

Page 15: Chapter 3 : User Datagram Protocol (UDP)

MulticastSocket

• A class that use connection-less oriented protocol to connect multiple network device to form a group.

• Data, File or any media can be send by 1 party to be received by multiple recipient.

Page 16: Chapter 3 : User Datagram Protocol (UDP)

MulticastSocket

Page 17: Chapter 3 : User Datagram Protocol (UDP)

Multicast Socket

• MulticastSocket class is a subclass from DatagramSocket – public class MulticastSocket extends DatagramSocket

• Calling class without parameter – public MulticastSocket() throws SocketException

• Calling class with parameter port – public MulticastSocket(int port) throws SocketException

• Calling class with parameter SocketAddress – public MulticastSocket(SocketAddress bindAddress) throws

IOException

Page 18: Chapter 3 : User Datagram Protocol (UDP)

Multicast Socket

• Uses existing DatagramPacket object to form the data need to be transmit

• MulticastSocket object is created on the server or receiver side – Example

• Later the object is bind to the server IP as central grouping point – Example