Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 8 January 2018

Java program to find host IP address

You can get IP address of any host by using InetAddress class. By calling getByName() method with host name as parameter, it returns InetAddress object. On this object you can call getHostAddress() method to get the IP address of the given host.


import java.net.InetAddress;
import java.net.UnknownHostException;
public class MyIpByHost {
public static void main(String a[]){
try {
InetAddress host = InetAddress.getByName("www.m4513.blogspot.com");
System.out.println(host.getHostAddress());
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}

No comments:

Post a Comment