Datagrams:DatagramSocket Class

DatagramSocket class represents connectionless socket for sending and receiving datagram packets.

A Datagram is basically an information but there is no guarantee of its content ,arrival or arrival time.

Constructor

Method                                           Description
DatagramSocket() It creats a datagram socket and bind its with the available port number on the local host machine
DatagramSocket(int port) It creats a datagram socket and bind its with the given port number
DatagramSocket(int port,inetAddress address) It creats a datagram socket and bind its with the specified port number and host address

 

Datagrams : DatagramPacket Class

Java DatagramPacket is message that can be sent or received

If you send multiple packets it may arrive in any order

Additionally packet delivery is not guaranteed

 

Constructors

Method                                                                       Description
DatagramPacket(byte[] bfr,int length) It creats a datagram packet ,this constructor is used to receive the packets
 DatagramPacket(byte[] bfr,int length,InetAddress address,int port) It creats a datagram packet ,this constructor is used to send the packets
 DatagramPacket(byte[] bfr,int offset,int length) It creats a datagram  packet , for receiving packets of length length, specifying an offset into the buffer.
DatagramPacket(byte[] bfr,int offset,int length,InetAddress  address,int port) It creates datagram packet for sending packets of length length with offset offset to the specified port number on the specified host.
 DatagramPacket(byte[] bfr,int offset,int length,SocketAddress address) It creates datagram packet  for sending packets of length length with offset ioffset to the specified port number on the specified host.
 DatagramPacket(byte[] bfr,int length,SocketAddress  address) It creates datagram packet for sending packets of length length to the specified port number on the specified host.

 

Send DatagramPacket by DatagramSocket

import java.io.*;
import java.net.*;

public class SendData {

    public static void main(String[] args) throws Exception { 

        DatagramSocket ds = new DatagramSocket(); 

        String str = "Message Sent by Datagram Socket"; 

        InetAddress ip = InetAddress.getByName("127.0.0.1"); 

        DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000); 

        ds.send(dp); 

        ds.close(); 

      }                

}

 

Recieve DatagramPacket by DatagramSocket

 

import java.io.*;
import java.net.*;

public class receiveData {

   public static void main(String[] args) throws Exception { 

        DatagramSocket dsocket = new DatagramSocket(3000); 

        byte[] buf = new byte[1024]; 

        DatagramPacket dpacket = new DatagramPacket(buf, 1024); 

        dsocket.receive(dpacket); 

        String str = new String(dpacket.getData(), 0, dpacket.getLength()); 

        System.out.println(str); 

        ds.close(); 

      }                

}
Output:
Message Sent by Datagram Socket