java getLocalAddress() 返回 0.0.0.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10298480/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:26:32  来源:igfitidea点击:

getLocalAddress() returning 0.0.0.0

javasocketsudp

提问by fhbeltrami

I am trying to write a program using Sockets and I need to get my own local IP address.

我正在尝试使用套接字编写程序,我需要获取自己的本地 IP 地址。

When I use getLocalAddress in the socket, I only get 0.0.0.0.

当我在套接字中使用 getLocalAddress 时,我只得到 0.0.0.0。

Here is a little piece of my code:

这是我的一小段代码:

DatagramSocket socket;
DatagramPacket pacoteEnvio = new DatagramPacket(msgByte, msgByte.length, addr, 6500);
socket = new DatagramSocket();
System.out.println("Local address = " + socket.getLocalAddress());
socket.send(pacoteEnvio);

Do you have any idea?

你有什么主意吗?

I am using UDP, so I am not sure if I can get my IP this way because it's connectionless, but I think you can help me!

我正在使用 UDP,所以我不确定我是否可以通过这种方式获取我的 IP,因为它是无连接的,但我认为您可以帮助我!

回答by Petesh

Getting the local address using mechanisms like this generally doesn't work in the way you expect. A system generally has at least two addresses - 127.0.0.1and ip address of nic, when you bind to an address for listening, you are binding to INADDR_ANY, which is the same as the address 0.0.0.0which is the same as binding to 127.0.0.1andip address of nic. Consider a laptop with a wired and wireless network card - either or both of them could be connected at the same time. Which would be considered the IP address of the system in this case?

使用这样的机制获取本地地址通常不会以您期望的方式工作。一个系统通常至少有两个地址 -127.0.0.1并且ip address of nic,当您绑定到一个地址进行侦听时,您将绑定到 INADDR_ANY,这0.0.0.0与绑定到127.0.0.1的地址相同ip address of nic。考虑一台带有有线和无线网卡的笔记本电脑 - 它们可以同时连接一个或两个。在这种情况下,哪个将被视为系统的 IP 地址?

I stole a subset of the answer for enumerating the ip addresses of all enabled NIC cards, which deals with the addresses of all the NICs, which lists the IP addresses for all the interfaces, on my system I have 10 interfaces, so I end up with a lot of IP addresses.

我窃取了枚举所有启用的 NIC 卡的 ip 地址的答案的子集,它处理所有 NIC 的地址,列出所有接口的 IP 地址,在我的系统上我有 10 个接口,所以我最终有很多IP地址。

try {
  System.out.println("Full list of Network Interfaces:");
  for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
      NetworkInterface intf = en.nextElement();
      System.out.println("    " + intf.getName() + " " + intf.getDisplayName());
      for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
          System.out.println("        " + enumIpAddr.nextElement().toString());
      }
  }
} catch (SocketException e) {
  System.out.println(" (error retrieving network interface list)");
}

Generally, though if you're server programming, when you receive a packet on a UDP service, it contains the sender's IP address, which you simply send the response to, and the computer is smart enough to send it out of the correct network interface.

一般来说,虽然如果你是服务器编程,当你在 UDP 服务上收到一个数据包时,它包含发件人的 IP 地址,你只需将响应发送到该地址,并且计算机足够聪明,可以将它从正确的网络接口发送出去.

回答by kaiweiz

I am working on a similar project - a bellford man client project. I tried your way at first and got 0.0.0.0 too. Then I figured it out by just using a method of InetAddress to output the address as a string - without involving the datagram socket.

我正在做一个类似的项目 - 一个 bellford man 客户项目。我一开始尝试了你的方法,也得到了 0.0.0.0。然后我通过使用 InetAddress 的方法将地址输出为字符串来解决它 - 不涉及数据报套接字。

String localIP = InetAddress.getLocalHost().getHostAddress();
System.out.println(localIP);

And I displayed my internal IP - the IP assigned my the router. Which was enough for me. Hope this helps.

我显示了我的内部 IP - 分配给我的路由器的 IP。这对我来说已经足够了。希望这可以帮助。

回答by Nikhar

You can try :

你可以试试 :

InetAddress ip = InetAddress.getLocalHost();

InetAddress ip = InetAddress.getLocalHost();

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/DatagramSocket.html

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/DatagramSocket.html

public DatagramSocket(int port, InetAddress laddr) throws SocketException

Creates a datagram socket, bound to the specified local address. The local port must be between 0 and 65535 inclusive. If the IP address is 0.0.0.0, the socket will be bound to the wildcard address, an IP address chosen by the kernel.

public DatagramSocket(int port, InetAddress ladr) 抛出 SocketException

创建一个数据报套接字,绑定到指定的本地地址。本地端口必须介于 0 和 65535 之间(包括 0 和 65535)。如果 IP 地址为 0.0.0.0,则套接字将绑定到通配符地址,即内核选择的 IP 地址。

回答by Stephen C

The javadoc for DatagramSocket.getLocalAddress()says this

javadoc forDatagramSocket.getLocalAddress()说这个

Returns - the local address to which the socket is bound, nullif the socket is closed, or an InetAddressrepresenting wildcard address if either the socket is not bound, or the security manager checkConnectmethod does not allow the operation.

返回 - 套接字绑定到的本地地址(null如果套接字已关闭)或InetAddress表示通配符地址(如果套接字未绑定或安全管理器checkConnect方法不允许该操作)。

If you create a DatagramSocketusing the no-args constructor, you get a socket that is not bound, and therefore (as per the javadoc) getLocalAddress()should return the wildcard IP address 0.0.0.0. And it does.

如果DatagramSocket使用 no-args 构造函数创建 a ,则会得到一个未绑定的套接字,因此(根据 javadoc)getLocalAddress()应返回通配符 IP 地址0.0.0.0。确实如此。

It is surprising what you can learn by reading the documentation :-)

通过阅读文档可以学到的东西令人惊讶:-)

回答by Zhivko Draganov

While the DatagramSocket is not connected, it doesn't bind any port on local machine so it's IP is 0.0.0.0 . Try to make connection with socket.connect(SocketAddress addr)and then you will get the right result. You will get the same 0.0.0.0 ip adress after socket.disconnect()method.

虽然 DatagramSocket 没有连接,但它没有绑定本地机器上的任何端口,所以它的 IP 是 0.0.0.0 。尝试与 建立联系socket.connect(SocketAddress addr),然后您将获得正确的结果。方法后您将获得相同的 0.0.0.0 ip 地址socket.disconnect()