我创建了一部分获取本地主机和当前 IP 地址的代码,我在 windows、linux( opensuse
、debian
)、mac 上尝试这个,每个人都返回他的 IP 地址,除了返回的 debian127.0.0.1
并且我连接到互联网。
因为我期望以下地址
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
任何帮助。
我的代码是用 java 编写的,并在不同的平台上进行了测试。
我inetaddress
在java中使用类来编写代码
编辑 :
sbin/ipconfig 输出
eth0 Link encap:Ethernet HWaddr 00:1e:33:e0:56:af
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21e:33ff:fee0:56af/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10549 errors:0 dropped:0 overruns:0 frame:0
TX packets:12135 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8705530 (8.3 MiB) TX bytes:1857590 (1.7 MiB)
Interrupt:20 Memory:fc600000-fc620000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:270 errors:0 dropped:0 overruns:0 frame:0
TX packets:270 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:22696 (22.1 KiB) TX bytes:22696 (22.1 KiB)
vmnet1 Link encap:Ethernet HWaddr 00:50:56:c0:00:01
inet addr:192.168.68.1 Bcast:192.168.68.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:159 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
vmnet8 Link encap:Ethernet HWaddr 00:50:56:c0:00:08
inet addr:172.16.79.1 Bcast:172.16.79.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fec0:8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:45 errors:0 dropped:0 overruns:0 frame:0
TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wlan0 Link encap:Ethernet HWaddr 70:1a:04:a5:86:5a
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
等/主机
127.0.0.1 localhost
127.0.1.1 debiam.domain debiam
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
关于 src 代码它只是inetaddress