InetAddress class example in java


          This class represents an Internet protocol address. An instance of InetAddress consists consists of IP addresses and possibly its corresponding host name. This class provides methods to resolve host names and their IP addresses and vice-versa. This class uses Domain Name Systems (DNS) to return information about a host.
The major useful methods in this class are,

getByName(host)   - returns the IP of the host.
getLocalHost ()      - returns the IP and name of the computer.
getHostName()      -  returns host of the IP address.
getHostAddress()   -  returns IP address of the host

Below is a very simple example for InetAddress class in java

The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class.


InetAdressExample.java

import java.net.*;

public class InetAddressExample {
    public static void main(String args[]) throws UnknownHostException {
        InetAddress ip_addr = InetAddress.getByName("www.compiletimeerror.com");
        System.out.println("CTE : " + ip_addr);
        System.out.println("CTE : " + ip_addr.getHostAddress());
        System.out.println("CTE : " + ip_addr.getHostName());

    }
}

Output

CTE : www.compiletimeerror.com/74.125.135.121
CTE : 74.125.135.121
CTE : www.compiletimeerror.com

 



Reactions:

0 comments :

Post a Comment