InetAddress and Datagram Class In Java

Introduction

 
In this article, we discuss the InetAddress, DatagramSocket and DatagramPacket classes in Java.
 

InetAdress class

 
The java.net.InetAddress class represents an IP address. The Inet Address class provides methods to get the IP of any hostname.
 

Some commonly used public methods are:

 
static InetAddress getByName (String host) throws UnknownHostException
        used to return the IP of the given host.
 
static InetAddress getLocalHost() throws UnknownHostException
        used to return the LocalHost IP and name.
 
String getHostName()
        used to return the hostname of the IP address.
 
String getHostAddress()
        used to return the IP address in string format.
 
Example
  1. import java.net.*;  
  2. import java.io.*;  
  3. public class InetEx {  
  4.  public static void main(String[] args) {  
  5.   try {  
  6.    InetAddress ipadd = InetAddress.getByName("www.c-sharpcorner.com");  
  7.    System.out.println("Host Name Of Given Host: " + ipadd.getHostName());  
  8.    System.out.println("IP Address Of Given Host: " + ipadd.getHostAddress());  
  9.   } catch (Exception ey) {  
  10.    System.out.println(ey);  
  11.   }  
  12.  }  
  13. }  
Output
 
InetEx.jpg
 

DatagramSocket class (Networking)

 
This class shows a connection-less socket for receiving and sending datagram packets. Datagrams are basically information so there is no guarantee of its content, arrival or arrival time. The DatagramPacket classes and DatagramSocket are used for connection-less socket programming.
 

Some commonly used constructors are:

 
DatagramSocket(int portno) throws SocketEeption
            creates a datagram socket and bind it with the given Port number.
 
DatagramSocket() throws SocketEeption
            creates a datagram socket and bind it with the available Port Number on the localhost machine.
 
DatagramSocket(int port, InetAddress address)throws SocketEeption
            creates a datagram socket and bind it with the specified port number and host address.
 

DatagramPacket class

 
The DatagramPacket is a type of message that can be sent and received. If we send multiple packets then it may arrive in any order. Moreover, packet delivery is not guaranteed.
 

Some commonly used constructors are:

 
DatagramPacket (byte[] barr, int length)
            creates a datagram packet. This constructor is used to receive the packets.
 
DatagramPacket (byte[] barr, int length, InetAddress address, int port)
            creates a datagram packet. This constructor is used to send the packets.
 
Example
 
In this example, we show how a DatagramPacket is sent using DatagramSocket.
 
DataSender.java
  1. import java.net.DatagramSocket;  
  2. import java.net.InetAddress;  
  3. import java.net.DatagramPacket;  
  4. public class DataSender {  
  5.  public static void main(String[] args) throws Exception {  
  6.   DatagramSocket dgskt = new DatagramSocket();  
  7.   String str = "Welcome";  
  8.   InetAddress intadd = InetAddress.getByName("127.0.0.1");  
  9.   DatagramPacket dgpkt = new DatagramPacket(str.getBytes(), str.length(), intadd, 3000);  
  10.   dgskt.send(dgpkt);  
  11.   dgskt.close();  
  12.  }  
  13. }  
DataReceiver.java
  1. import java.net.DatagramPacket;  
  2. import java.net.DatagramSocket;  
  3. public class DataReceiver {  
  4.  public static void main(String[] args) throws Exception {  
  5.   DatagramSocket dgskt = new DatagramSocket(3000);  
  6.   byte[] bf = new byte[1024];  
  7.   DatagramPacket dgpkt = new DatagramPacket(bf, 1024);  
  8.   dgskt.receive(dgpkt);  
  9.   String strRecv = new String(dgpkt.getData(), 0, dgpkt.getLength());  
  10.   System.out.println(strRecv);  
  11.   dgskt.close();  
  12.  }  
  13. }