Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get my ip address? [duplicate]

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?

like image 905
supergiox Avatar asked Aug 17 '11 00:08

supergiox


People also ask

How do I find a duplicate IP address?

Here is how you can check it: On an unaffected host on the same network, open up a command prompt. On a Windows machine, type "arp -a [suspected duplicate IP]" and hit enter. On a Mac or Linux machine, type "arp [suspected duplicate IP]" and hit enter.

Can you get the same IP address twice?

An IP address must be unique across your network. The Dynamic Host Configuration Protocol (DHCP) server cannot assign a single IP address to more than one client.

Why does IP address say duplicate?

NOTE: A Duplicate IP Address error message occurs when another device, such as a Laptop, Desktop, Cell Phone or Tablet has connected to the Network, and obtained the same IP Address from the DHCP Server that is in use by the Printer.


2 Answers

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

like image 59
brendan Avatar answered Sep 29 '22 11:09

brendan


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

like image 26
Satish Avatar answered Sep 29 '22 11:09

Satish