|
以下实例演示了如何使用 InetAddress 类的 getLocalAddress() 方法获取本机IP地址及主机名: 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 ) ;
}
} |
以上代码运行输出结果为:
Local HostAddress: 192.168.1.4
Local host name: harsh
程序猿的技术大观园:www.javathinker.net
[这个贴子最后由 flybird 在 2020-01-26 12:03:24 重新编辑]
|
|