Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client

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?

like image 352
Touchstone Avatar asked Mar 21 '12 16:03

Touchstone


People also ask

What is 10. 0 2. 2 Android?

The main take-away is that the IP Address 10.0. 2.2 is a special alias to your host loop-back interface (i.e., 127.0. 0.1 on your development machine). To access your development machine you will use the 10.0. 2.2 IP Address from the browser running on the Android emulator.

What is the localhost for Android emulator?

If you're using Android Studio to run the emulator, then localhost of your host computer will be mapped to the IP address, 10.0. 2.2 , inside the emulator. If you're using other programs to run the emulator, then you may need to consult the documentation associated with those programs.


2 Answers

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 
like image 72
Zephyr Avatar answered Sep 19 '22 01:09

Zephyr


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

like image 33
vlad_o Avatar answered Sep 23 '22 01:09

vlad_o