Câu hỏi của tôi tương tự như câu hỏi this. Tôi muốn nhận được IP thật của máy tính của tôi (không 127.0.0.1) nhưng kỳ lạ, mã dưới đây vào Ubuntu của tôi đang trở lại 127.0.1.1InetAddress.getLocalHost(). GetHostAddress() đang trả về 127.0.1.1
InetAddress.getLocalHost().getHostAddress()
Dưới đây là mã hoàn chỉnh của tôi, ban đầu được đăng tại SO tại here
127.0.1.1public String getMachineIP() {
try {
String hostIP = InetAddress.getLocalHost().getHostAddress();
if (!hostIP.equals("127.0.0.1")) {
return hostIP;
}
/*
* Above method often returns "127.0.0.1", In this case we need to
* check all the available network interfaces
*/
Enumeration<NetworkInterface> nInterfaces = NetworkInterface
.getNetworkInterfaces();
while (nInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAddresses = nInterfaces
.nextElement().getInetAddresses();
while (inetAddresses.hasMoreElements()) {
String address = inetAddresses.nextElement()
.getHostAddress();
if (!address.equals("127.0.0.1")) {
return address;
}
}
}
} catch (UnknownHostException e1) {
System.err.println("Error = " + e1.getMessage());
} catch (SocketException e1) {
System.err.println("Error = " + e1.getMessage());
}
return null;
}
Đoạn mã trên đang trở lại trong khi ifconfig
trên máy tính Ubuntu của tôi là đưa ra dưới đây sản lượng
[email protected]:~# ifconfig
eth0 Link encap:Ethernet HWaddr 00:21:70:b7:30:cd
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)
Interrupt:28 Base address:0x6000
eth1 Link encap:Ethernet HWaddr 00:22:68:d3:02:b5
inet addr:192.168.2.112 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::222:68ff:fed3:2b5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:23827 errors:0 dropped:0 overruns:0 frame:32515
TX packets:23200 errors:46 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:22027719 (22.0 MB) TX bytes:3778268 (3.7 MB)
Interrupt:19
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:402 errors:0 dropped:0 overruns:0 frame:0
TX packets:402 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:29197 (29.1 KB) TX bytes:29197 (29.1 KB)
tôi thấy entry 127.0.1.1 trong hos t tệp (Lạ với tôi, vì tôi chưa bao giờ cập nhật tệp này)
[email protected]:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 dell
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Làm cách nào để nhận IP thực sự của máy (không phải 127.0.0.1)? Tôi chỉ tìm kiếm địa chỉ IPv4 trừ 127.0.0.0/8 subnet
Điều gì sẽ xảy ra nếu bạn bỏ qua 127.0.1.1 (tự hỏi) – ssedano
Điều này có hữu ích không? http://stackoverflow.com/q/8083479/1039920 – Thevenin
Không đánh dấu là trùng lặp ... Tôi nhận được '127.0.1.1' không' 127.0.0.1' –