I am writing an application where I need the IP address. I have a domain name and I would like to know how to get the IP address from it. For example, "www.girionjava.com". How could I get the IP address of this website by programming in Java? Thanks.
5 Answers
This should be simple.
InetAddress[] machines = InetAddress.getAllByName("yahoo.com"); for(InetAddress address : machines){ System.out.println(address.getHostAddress()); } 1 Comment
Joehot200
Does this get all IPs on a round-robin DNS?
(Extra mask in printing sine java considers all integers to be signed, but an IP address is unsigned)
InetAddress[] machines = InetAddress.getAllByName("yahoo.com"); for(InetAddress address : machines){ byte[] ip = address.getAddress(); for(byte b : ip){ System.out.print(Integer.toString(((int)b)&0xFF)+"."); } System.out.println(); } 1 Comment
Joachim Sauer
This assumes that you'll be getting only IPv4 adresses. IPv6 adresses are formatted differently, so you shouldn't manually format it anyway.
The InetAddress class in java.net has some static methods to do this.
InetAddress a = InetAddress.getByName ("www.girionjava.com"); System.out.println(a.getHostAddress()); This is very basic stuff, for more complete control over DNS queries, you will have to use additional libraries.
1 Comment
Adriaan
Welcome to Stack Overflow! Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.