Java 實(shí)例 - 獲取本機(jī)ip地址及主機(jī)名
以下實(shí)例演示了如何使用 InetAddress 類的 getLocalAddress() 方法獲取本機(jī)ip地址及主機(jī)名:
/* author by w3cschool.cn Main.java */ import java.net.InetAddress; public class Main { public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress: "+addr.getHostAddress()); String hostname = addr.getHostName(); System.out.println("Local host name: "+hostname); } }
以上代碼運(yùn)行輸出結(jié)果為:
Local HostAddress: 192.168.1.4 Local host name: harsh
更多建議: