8

I've a serverSocket and I would like to know the IP address, but with

listenSocket.getInetAddress().toString();

I get 0.0.0.0 . How can I get the IP address, or (if there are two connections enabled) one of them?

Matt
  • 22,721
  • 17
  • 71
  • 112
supergiox
  • 1,586
  • 7
  • 19
  • 27

3 Answers3

15

I've used this in the past:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

Source: http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

Elhanan Mishraky
  • 2,736
  • 24
  • 26
brendan
  • 29,308
  • 20
  • 68
  • 109
  • 1
    Correct answer. I think inetAddress.isSiteLocalAddress() would be a better choice than !inetAddress.isLoopbackAddress(). – Craig B Aug 24 '12 at 12:57
  • @CraigB: I like your thinking--isSiteLocalAddress() is a little easier for a person to digest. But using it, I always get 127.0.0.1, the "self" ip. So I recommend sticking to isLoopbackAddress(). – SMBiggs Aug 15 '13 at 07:52
  • As @SatishKumar pointed out in another answer, you'll want to use InetAddressUtils.isIPv4Address() to check the IP, if you want only that. Without this, you can receive an IPv6. – Guillaume Boudreau Apr 12 '14 at 17:07
  • Didn't work so well for me, only gave me IPv6. This one was more helpful http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device – Rudolf Real Dec 04 '14 at 20:14
2

Please check this How to get IP address of the device from code? getting ipaddress needs following check also InetAddressUtils.isIPv4Address(sAddr);

Community
  • 1
  • 1
Satish
  • 320
  • 2
  • 9
0

If you get 0.0.0.0, that is probably because your listening socket is accepting connections on all of the machine's interfaces. Typically there'll be at least two of them -- one for communicating with the outside world, and the localhost/loopback interface. So it doesn't make clear sense to ask for one address for it.

Once you accept an incoming connection, you can ask it the address of the particular interface that's handling it.

A quick trick for finding a "good' IP address is to make an outgoing connection to a known site, and ask for the address of its local end once it connects. This is somewhat more portable than enumerating network interfaces (if you care about such things), but of course requires that you have something to connect to that you trust to be alive and reachable.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
  • Is there a link to an SO answer that includes some sample code to do this ? There seem to be a lot of discussions on this topic. I'm trying to locate where my desktop application is running. I have the geo-locator code working but need the users external IP. Thanks. – Will Oct 01 '12 at 22:10
  • @Will: In general these solutions (my answer or the other one) will only tell you the _local_ IP, which will often be a NATted address in an RFC-1918 network. – hmakholm left over Monica Oct 01 '12 at 23:06
  • That's what I discovered also. I think I will post a fresh question. – Will Oct 02 '12 at 16:58