Java获取本地IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19476872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java get local IP
提问by Nico Hauser
Im trying to get the local IP. It should work with
我正在尝试获取本地 IP。它应该与
System.out.println(Inet4Address.getLocalHost().getHostAddress());
or
或者
InetAddress addr = InetAddress.getLocalHost();
ip = addr.getHostAddress();
System.out.println("Ip: " + ip);
but it always returns 192.168.178.154
instead of 192.168.178.119
(This is my real local IP(Terminal --> ifconfig
))
但它总是返回192.168.178.154
而不是192.168.178.119
(这是我真正的本地 IP(终端 --> ifconfig
))
What should I do?
我该怎么办?
采纳答案by Maxim Shoustin
Sounds like you have two IP addresses.
听起来您有两个 IP 地址。
On a computer that has one network adapter, the IP address that is chosen is the Primary IP address of the network adaptor in the computer. However, on a multiple-homed computer, the stack must first make a choice. The stack cannot make an intelligent choice until it knows the target IP address for the connection.
When the program sends a connect() call to a target IP address, or sends a send() call to a UDP datagram, the stack references the target IP address, and then examines the IP route table so that it can choose the best network adapter over which to send the packet. After this network adapter has been chosen, the stack reads the Primary IP address associated with that network adapter and uses that IP address as the source IP address for the outbound packets.
在具有一个网络适配器的计算机上,选择的 IP 地址是计算机中网络适配器的主 IP 地址。但是,在多宿主计算机上,堆栈必须首先做出选择。堆栈在知道连接的目标 IP 地址之前无法做出明智的选择。
当程序向目标 IP 地址发送 connect() 调用,或向 UDP 数据报发送 send() 调用时,堆栈引用目标 IP 地址,然后检查 IP 路由表,以便选择最佳网络发送数据包的适配器。选择此网络适配器后,堆栈会读取与该网络适配器关联的主 IP 地址,并将该 IP 地址用作出站数据包的源 IP 地址。
If you want to activate second IP and its for example LAN, unplug it and after 10 sec plug in back. Other IP might be selected as host IP in routing table.
如果您想激活第二个 IP 及其例如 LAN,请拔下它并在 10 秒后重新插入。在路由表中可能会选择其他 IP 作为主机 IP。
You can get 2nd IP from getNetworkInterfaces
.
您可以从getNetworkInterfaces
.
Try to run followed code:
尝试运行以下代码:
public static void main(String[] args) throws Exception
{
System.out.println("Your Host addr: " + InetAddress.getLocalHost().getHostAddress()); // often returns "127.0.0.1"
Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
for (; n.hasMoreElements();)
{
NetworkInterface e = n.nextElement();
Enumeration<InetAddress> a = e.getInetAddresses();
for (; a.hasMoreElements();)
{
InetAddress addr = a.nextElement();
System.out.println(" " + addr.getHostAddress());
}
}
}
回答by Prabhakaran Ramaswamy
if your system is configured with multiple ip then do like this.
如果您的系统配置了多个 ip,那么请这样做。
try {
InetAddress inet = InetAddress.getLocalHost();
InetAddress[] ips = InetAddress.getAllByName(inet.getCanonicalHostName());
if (ips != null ) {
for (int i = 0; i < ips.length; i++) {
System.out.println(ips[i]);
}
}
} catch (UnknownHostException e) {
}