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 host name.
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() thows UnknownHostException
used to return the LocalHost IP and name.
String getHostName()
used to return the host name of the IP address.
String getHostAddress()
used to return the IP address in string format.
Example
import java.net.*;
import java.io.*;
public class InetEx
{
public static void main(String[] args)
{
try
{
InetAddress ipadd=InetAddress.getByName("www.c-sharpcorner.com");
System.out.println("Host Name Of Given Host: "+ipadd.getHostName());
System.out.println("IP Address Of Given Host: "+ipadd.getHostAddress());
}
catch(Exception ey)
{
System.out.println(ey);
}
}
}
Output
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.
Constructors
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.
Constructors
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 usign DatagramSocket.
DataSender.java
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.DatagramPacket;
public class DataSender
{
public static void main(String[] args) throws Exception
{
DatagramSocket dgskt = new DatagramSocket();
String str = "Welcome";
InetAddress intadd = InetAddress.getByName("127.0.0.1");
DatagramPacket dgpkt = new DatagramPacket(str.getBytes(), str.length(), intadd, 3000);
dgskt.send(dgpkt);
dgskt.close();
}
}
DataReceiver.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class DataReceiver
{
public static void main(String[] args) throws Exception
{
DatagramSocket dgskt = new DatagramSocket(3000);
byte[] bf = new byte[1024];
DatagramPacket dgpkt = new DatagramPacket(bf, 1024);
dgskt.receive(dgpkt);
String strRecv = new String(dgpkt.getData(), 0, dgpkt.getLength());
System.out.println(strRecv);
dgskt.close();
}
}