I am trying to write a program using Sockets and I need to get my own local IP address.
When I use getLocalAddress in the socket, I only get 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!
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.1
and 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.0
which is the same as binding to 127.0.0.1
and ip 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?
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.
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.
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.
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.
You can 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 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.
The javadoc for DatagramSocket.getLocalAddress()
says this
Returns - the local address to which the socket is bound,
null
if the socket is closed, or anInetAddress
representing wildcard address if either the socket is not bound, or the security managercheckConnect
method does not allow the operation.
If you create a DatagramSocket
using 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.
It is surprising what you can learn by reading the documentation :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With