Problem Statement: Many times we need to find the host name and IP address of local and the remote machines. Most importantly I want to do this with a Java program.
Solution: Here is the main class, I have named it as HostNameFinder and to test these methods I've another class called as HostNameFinderTest.
Here is the test class.
And here is the output that I got.
Solution: Here is the main class, I have named it as HostNameFinder and to test these methods I've another class called as HostNameFinderTest.
/**
*
* Classname : HostNameFinder.java
* <b>HostNameFinder<b><br>
* <p>
* This class helps to get the host name and the IP addresses of the local machine and
* remote machines. It provides static methods that can be used to get the very specific
* information such as host name or the IP address.
* </p>
*
* @author Manohar Negi
* @version 1.0
* @since 1.0
* @see
* Copyright notice
*
* Revision History:
*
* Date By Version Comments
* ---------------------------------------------------------------------------------
* Aug 31, 2011 Manohar Negi 1.0 Initial Draft
* Sep 01, 2011 Manohar Negi 1.1 Modified to include more methods
*
**/
package com.mani.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* <p>
* <b>Functional Description:</b>
* <p>
* Some Description of the file
*
* Created: Month, Day ,YYYY
*
* @author
* @version
*/
public class HostNameFinder {
public static String getMyHostName() {
String hostname = null;
try {
InetAddress addr = InetAddress.getLocalHost();
hostname = addr.getHostName();
System.out.println("Host Name = " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostname;
}
public static String getMyIPAddress() {
String ipAddress = null;
try {
InetAddress addr = InetAddress.getLocalHost();
String ipAddr = addr.getHostAddress();
System.out.println("IP Address = " + ipAddr.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ipAddress;
}
public static String getIPByAddress(String address) {
String ipAddress = null;
try {
InetAddress addr = InetAddress.getByName(address);
String ipAddr = addr.getHostAddress();
System.out.println("IP Address = " + ipAddr.toString());
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ipAddress;
}
public static String getHostNameByAdress(String address) {
String hostname = null;
try {
InetAddress addr = InetAddress.getByName(address);
hostname = addr.getHostName();
System.out.println("Host Name = " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostname;
}
}
Here is the test class.
/**
*
* Classname : HostNameFinderTest.java
* <b>HostNameFinderTest<b><br>
* <p>
* This class is to test the methods of HostNameFinder.class
* </p>
*
* @author Manohar Negi
* @version 1.0
* @since 1.0
* @see
* Copyright notice
*
* Revision History:
*
* Date By Version Comments
* ---------------------------------------------------------------------------------
* Aug 31, 2011 Manohar Negi 1.0 Initial Draft
* Sep 01, 2011 Manohar Negi 1.1 Modified to include more methods
*
**/
package com.mani.util;
public class HotmNameFinderTest {
public static void main(String[] args) {
/* get the host name for local machine */
HostNameFinder.getMyHostName();
/* get the IP for local machine */
HostNameFinder.getMyIPAddress();
/* get the host name for localhost */
HostNameFinder.getHostNameByAdress("localhost");
/* get the IP for localhost */
HostNameFinder.getIPByAddress("localhost");
/* get the host name for www.google.com */
HostNameFinder.getHostNameByAdress("www.google.com");
/* get the IP for www.google.com */
HostNameFinder.getIPByAddress("www.google.com");
}
}
And here is the output that I got.
Host Name = mani-mac
IP Address = 192.168.2.4
Host Name = localhost
IP Address = 127.0.0.1
Host Name = www.google.com
IP Address = 74.125.236.83
Everyone loves what you guys tend to be up too. This type of clever work and coverage!
ReplyDeleteKeep up the awesome works guys I've you guys to blogroll.
my homepage; Real IP
Also see my web page > Hide IP
What's up to all, it's really a good for me to pay a quick visit this website, it contains useful Information.
ReplyDeleteMy web-site: sourceinternet.net
Its like you read my mind! You appear to know so much approximately this, such as you wrote the guide in it
ReplyDeleteor something. I feel that you simply could do with a few p.
c. to force the message house a little bit, but other than that,
this is excellent blog. An excellent read. I will certainly be back.
my page :: http://bestcloudcomputingoffers.com