86

I want to get the currently running Android Emulator's IP address through code. How can it be achieved?

hhh
  • 47,778
  • 59
  • 172
  • 269
Rajapandian
  • 9,127
  • 24
  • 72
  • 86
  • 1
    Perhaps related [here](http://stackoverflow.com/questions/7447221/how-to-connect-to-avd) and [here](http://stackoverflow.com/questions/13217796/is-android-avds-firewall-somehow-more-restricted-to-real-android-firewall). – hhh Nov 05 '12 at 13:29
  • @hhh Second link is broken. :) – Muhamed Huseinbašić Apr 17 '15 at 14:57
  • 1
    @MuhamedHuseinbašić I feel this thread solved the problem in the second thread so decided to delete it to keep things simple. – hhh Apr 20 '15 at 13:29

7 Answers7

176

Just to clarify: from within your app, you can simply refer to the emulator as 'localhost' or 127.0.0.1.

Web traffic is routed through your development machine, so the emulator's external IP is whatever IP has been assigned to that machine by your provider. The development machine can always be reached from your device at 10.0.2.2.

Since you were asking only about the emulator's IP, what is it you're trying to do?

Matthias
  • 42,418
  • 28
  • 102
  • 131
  • 10
    I want to upvote you 100 times for this. I couldn't figure out how my app could talk to another local server I had running until I read your post. Thanks! – Spike Jan 08 '11 at 02:21
  • How can you reach your development machine from 10.0.2.2? I tried pinging but nothing, pic [here](http://i.stack.imgur.com/Yjv7W.jpg). Ifconfig -app on the right and terminal-emulator on the left trying to ping dev-machine. Cannot see anything in localhost, any simple tests? – hhh Nov 05 '12 at 13:21
  • @hhh You can try JuiceSSH: https://play.google.com/store/apps/details?id=com.sonelli.juicessh You can reach your host from the mentioned IP or from the real IP of the host. Both variants work for me. – Muhamed Huseinbašić Apr 17 '15 at 15:11
  • I believe the traffic from an emulator is routed over a "virtual" router first (with IP address of `10.0.2.1`, not directly. So, there should be one extra connection takes place between this router `10.0.2.1` to the host/dev machine `127.0.0.1/10.0.2.2`. What I really usually change the port number I want to connect to. – stdout Apr 17 '20 at 09:35
39

If you do truly want the IP assigned to your emulator:

adb shell
ifconfig eth0

Which will give you something like:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]
Derek Shockey
  • 1,777
  • 16
  • 15
27

Like this:

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().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

Check the docs for more info: NetworkInterface.

Marcin Gil
  • 65,710
  • 8
  • 59
  • 60
21

Use this method you will be getting 100% correct ip address for your android emulator

To get the ip address of yoor emulator

Go to adb shell and type this command

adb shell
ifconfig eth0

enter image description here

After running this command I am getting

IP : 10.0.2.15

Mask : 255.255.255.0

Which works for me . I am also working for an networking application.

Nikhil Agrawal
  • 25,020
  • 19
  • 86
  • 120
  • The emulator no longer has 'eth0', but has 'radio0' and 'wlan0' instead. You may want to edit this answer, because it is no longer 100% correct. – Adam Howell Apr 07 '21 at 22:47
10

If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1) refers to its own loopback interface.More details: http://developer.android.com/guide/faq/commontasks.html#localhostalias

mahbub_siddique
  • 1,717
  • 18
  • 22
3
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().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}
Nikhil Agrawal
  • 25,020
  • 19
  • 86
  • 120
den
  • 41
  • 1
0

Within the code of my app I can get the running device IP andress easily like beolow:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Blasanka
  • 18,202
  • 10
  • 89
  • 96