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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");
Then, if you want the IP as a String
String address = giriAddress.getHostAddress();
回答2:
InetAddress.getByName("www.girionjava.com")
回答3:
This should be simple.
InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
System.out.println(address.getHostAddress());
}
回答4:
(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();
}