Sunday, July 22, 2018

Very useful and Mind blowing API

Lets get some knowledge about 'InetAddress'

1. It is a pre-defined-class in java.net package

2. It is mandatory to import java.net package to use this class


3. This class is used to denote an IP address, also it is used to translate string into IP address and vice versa


4. Object of InetAddress stores IP address along with machine name 



How to get object of InetAddress class to denote IP address of my machine (my machine bole toh localhost) 

>> InetAddress referenceVariable = InetAddress.getLocalHost()


How to get object of InetAddress class to denote IP address of any machine which is connected to a network


>> InetAddress referenceVariable = InetAddress.getByName("here we can pass the hostname or IP address of any machine")


Note: We can't create object of  InetAddress class using constructor, because its constructor is private

Methods of InetAddress class ==>>


1. public String getHostName() <<== this method is used to fetch the name of machine

denoted by InetAddress

2. public String getHostAddress() <<== this method is used to fetch the IP address of machine denoted by InetAddress


--- Here are some codes to show the the functionality of InetAddress class ---

<< save this clas inside InetAddressDemoOne .java file >>

import java.net.InetAddress;

public class InetAddressDemoOne 
{
public static void main(String[] args) 
{
try 
{
// get the object of InetAddress
InetAddress ref = InetAddress.getLocalHost();

// get the name of machine
String host = ref.getHostName();
System.out.println("Hostname is "+host);

// get the IP address of machine
String ip = ref.getHostAddress();
System.out.println("Ip address is "+ip);

catch (Exception e) 
{
System.out.println("Runtime error in code "+e);
}
}

}

Output of code is:
Hostname is admin-PC

Ip address is 192.168.0.3


<< save this clas inside InetAddressDemoTwo .java file >>

import java.net.InetAddress;
import java.util.Scanner;

public class InetAddressDemoTwo 
{
public static void main(String[] args) 
{
try 
{
// create object of Scanner to take input from keyboard
Scanner sc = new Scanner(System.in);

System.out.print("Input machine name or ip address:");
String text = sc.nextLine();

// get the object of InetAddress to denote any machine
// it will be done using getByName() method
// note: make sure the machine is connected on a network 
// note: if the machine is not connected to network
// this method will return localhost or 127.0.0.1 
// (bole toh this computer)
InetAddress ref = InetAddress.getByName(text);

// get the name of machine
String host = ref.getHostName();
System.out.println("Hostname is "+host);

// get the IP address of machine
String ip = ref.getHostAddress();
System.out.println("Ip address is "+ip);

catch (Exception e) 
{
System.out.println("Runtime error in code "+e);
}
}

}

Output of code is:
Input machine name or ip address:<please provide name of machine>
Hostname is localhost
Ip address is 127.0.0.1

3 comments: