2

我创建了一部分获取本地主机和当前 IP 地址的代码,我在 windows、linux( opensusedebian)、​​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

4

2 回答 2

2

根据具体的实现,它将取决于您在 linux 中检查的接口。lo 接口将为您提供 127.0.0.1。您可能有一个像“eth0”或“eth3”这样的接口,它使用您预期的 IP 地址。

于 2012-09-05T18:24:14.320 回答
1

通过这个在 linux 中的推荐

ifconfig 

你给这个细节

eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx  
      inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::1e6f:65ff:fed5:8a16/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:3363918 errors:0 dropped:0 overruns:0 frame:0
      TX packets:1892239 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:4890880074 (4.8 GB)  TX bytes:150386146 (150.3 MB)
      Interrupt:43 Base address:0xa000 

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:160038 errors:0 dropped:0 overruns:0 frame:0
      TX packets:160038 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:64218501 (64.2 MB)  TX bytes:64218501 (64.2 MB)

wlan0     Link encap:Ethernet  HWaddr xx.xx.xx.xx.xx.xx  
      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)

ip 127.0.0.1 在 lo(loopback , localhost) 接口中,但在另一个接口中,例如 eth0 你的 ip 是 xxx.xxx.xxx.xxx 或 ipv6 。

于 2012-09-06T08:56:32.633 回答