85

We normally use 10.0.2.2:(port number) in the url to connect to the local web server,but we should use the computer's ip address instead of 10.0.2.2

Then why do we use 10.0.2.2?

3
  • Maybe 10.0.0.2 IS the computer ip address?
    – miniBill
    Mar 21, 2012 at 16:16
  • @Blackbelt the link is broken..please update it
    – Nike15
    Sep 24, 2016 at 9:47
  • @minBill 10.0.0.2 is not the computer IP address, it is a Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
    – Saharsh
    Oct 12, 2021 at 9:10

4 Answers 4

98

Network Address Space

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

The virtual router for each instance manages the 10.0.2/24 network address space — all addresses managed by the router are in the form of 10.0.2.<xx>, where <xx> is a number. Addresses within this space are pre-allocated by the emulator/router as follows:

Network Address Description

10.0.2.1    Router/gateway address
10.0.2.2    Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
10.0.2.3    First DNS server
10.0.2.4 / 10.0.2.5 / 10.0.2.6  Optional second, third and fourth DNS server (if any)
10.0.2.15   The emulated device's own network/ethernet interface
127.0.0.1   The emulated device's own loopback interface
2
36

If you are running both server and emulator in you computer 127.0.0.1:(port) will refer to the emulator itself and not to the server.The 10.0.2.2 is the solution to that problem :)

Hope that helped

25

The reason why you use 10.0.2.2 is because it's the special alias to your host loopback interface (aka 127.0.0.1). That's why it works, and isn't it cool that it does?

0
14

If you use 10.0.2.2 for connection it will always point to your localhost. I used the real IP for my machine before reading this and it works in both ways.

The advantage of using 10.0.2.2 is that you don't care what is the real IP and you can move the project easier on another computer.

3
  • Another advantage of using 10.0.2.2 is that it also works if your machine is not connected to the internet. Jan 22, 2017 at 12:24
  • 2
    Hi, appreciate that this is an old answer but im trying the same with a physical device connected via USB, but using 10.0.2.2 is not working. Would you know how to do the same with a physical device ?
    – Richlewis
    Dec 6, 2017 at 20:34
  • Hi, I don't think it will. Emulator is running on your computer while a physical device is completely independent (different host). I'm not aware of a solution which doesn't involve connecting directly to your computer's IP address.
    – azertiti
    Dec 7, 2017 at 10:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.