GetLocalAddress () returns 0.0.0.0

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

When I use getLocalAddress on the socket, I get 0.0.0.0.

Here is a small snippet 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 ideas?

I use UDP, so I'm not sure if I can get my IP address this way because it is connectionless, but I think you can help me!

+6
source share
5 answers

Obtaining a local address using such mechanisms usually does not work as you expect. Usually the system has at least two addresses - 127.0.0.1 and ip address of nic , when you bind to the listening address, you bind to INADDR_ANY, which matches the address 0.0.0.0 , which matches the binding to 127.0.0.1 and ip address of nic . Consider a laptop with a wired and wireless network card - either one or both of them can be connected at the same time. What will be considered the IP address of the system in this case?

I stole a subset of the answer for listing the IP addresses of all allowed NICs , which looks at the addresses of all network adapters that list the IP addresses for all interfaces on my system, I have 10 interfaces, so I get a lot of IP addresses.

 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)"); } 

Typically, though, if you program the server when you receive the packet in the UDP service, it contains the IP address of the sender to which you simply send the response, and the computer is smart enough to send it from the correct network interface.

+5
source

You may try:

InetAddress ip = InetAddress.getLocalHost ();

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

public DatagramSocket (int port, InetAddress laddr) throws a 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, the IP address selected by the kernel.

+4
source

javadoc for DatagramSocket.getLocalAddress() says it

Returns - the local address to which the socket is bound, null if the socket is closed, or InetAddress representing a wildcard address if either the socket is not connected or the checkConnect security manager method allows the operation.

If you create a DatagramSocket using the no-args constructor, you get a socket that is not connected, and therefore (according to javadoc) getLocalAddress() should return an address wildcard IP address of 0.0.0.0 . And indeed it is.

It's amazing what you can find out by reading the documentation :-)

+2
source

I am working on a similar project - the Bellford client-client project. First I tried my way and got 0.0.0.0. Then I realized this by simply using the InetAddress method to output the address as a string - without using a datagram socket.

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

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

+2
source

While the DatagramSocket is not connected, it does not bind any ports on the local machine, so the IP address is 0.0.0.0. Try to establish a connection with socket.connect(SocketAddress addr) , and then you get the correct result. You will get the same ip address 0.0.0.0 after socket.disconnect() method.

0
source

Source: https://habr.com/ru/post/914032/


All Articles