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.
Asked
Active
Viewed 3.1k times
4 Answers
32
InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");
Then, if you want the IP as a String
String address = giriAddress.getHostAddress();
Powerlord
- 84,782
- 17
- 123
- 168
9
This should be simple.
InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
System.out.println(address.getHostAddress());
}
redoc
- 235
- 3
- 15
-
Does this get all IPs on a round-robin DNS? – Joehot200 Dec 30 '15 at 18:06
6
InetAddress.getByName("www.girionjava.com")
Joachim Sauer
- 291,719
- 55
- 540
- 600
flybywire
- 247,088
- 187
- 391
- 495
0
(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();
}
M. Jessup
- 7,916
- 1
- 27
- 29
-
2This assumes that you'll be getting only IPv4 adresses. IPv6 adresses are formatted differently, so you shouldn't manually format it anyway. – Joachim Sauer Mar 17 '10 at 13:22