您如何确定使用 Java 连接到 Internet 的网络接口?例如,我跑
InetAddress.getLocalHost().getHostAddress();
在 Eclipse 中,这正是我想要的,192.168.1.105。但是,如果我把它打包成一个 jar 文件并运行程序,代码会返回 169.254.234.50。对此进行调查,我发现这是我机器上 VMware 虚拟以太网适配器接口的 IP 地址。
有什么方法可以确定连接到互联网的接口,同时保持我的代码的可移植性?
接口比较
接口 [net4]
display name : Intel(R) Centrino(R) Ultimate-N 6300 AGN
MTU : 1500
loopback : false
point to point: false
up : true
virtual : false
multicast : true
HW address : 00 24 D7 2C 5F 70
INET address (IPv4): 192.168.1.105
host name : MyComputer
canonical host name : MyComputer
loopback : false
site local : true
any local : false
link local : false
multicast : false
reachable : true
接口 [eth5]
display name : VMware Virtual Ethernet Adapter for VMnet1
MTU : 1500
loopback : false
point to point: false
up : true
virtual : false
multicast : true
HW address : 00 50 56 C0 00 01
INET address (IPv4): 169.254.234.50
host name : MyComputer
canonical host name : MyComputer
loopback : false
site local : false
any local : false
link local : true
multicast : false
reachable : true
还有第三个 VMware 界面,站点 local=true 和链接 local=false,因此这些字段也没有任何帮助。