以下實例演示瞭如何使用InetAddress 類的InetAddress.getByName() 方法來獲取指定主機(網址)的IP地址:
/* author by w3cschool.cc GetIP.java */import java.net.InetAddress;import java.net.UnknownHostException;public class GetIP { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getByName ("www.w3cschool.cc"); } catch (UnknownHostException e) { System.exit(2); } System.out.println(address.getHostName() + "=" + address.getHostAddress()); System.exit(0); }}
以上代碼運行輸出結果為:
www.w3cschool.cc=222.73.134.120
以上就是Java 實例- 獲取指定主機的IP地址的內容,更多相關內容請關注PHP中文網(www.php.cn)!