Chapter 3 : User Datagram Protocol (UDP)
-
Upload
ministry-of-higher-education -
Category
Education
-
view
1.195 -
download
0
Transcript of Chapter 3 : User Datagram Protocol (UDP)
CHAPTER 3 : User Datagram Protocol (UDP)
By :
Mr. Asyran Zarizi Bin Abdullah
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
Layman Terms for 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
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
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
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
UDP Socket Structure
Did you remember About previous lab (MySend) And (MyReceiver)?
Example :
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
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)
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();
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];
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();
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.
MulticastSocket
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
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