Emulator networking ip address and virtual router

2020-07-17 16:37发布

问题:

I am using android emulator on windows. The android developer doc says that each instance of the emulator runs behind a virtual router/firewall service. The virtual router for each instance manages the 10.0.2/24 network address space . However, when I connect to the emulator with adb shell and do ifconfig, I got the following output:

radio0 Link encap:UNSPEC inet addr:192.168.200.2 Bcast:192.168.200.255 Mask:255.255.255.0

wlan0 Link encap:UNSPEC inet addr:192.168.232.2 Bcast:192.168.239.255 Mask:255.255.248.0

ip route show gives me the following output

192.168.200.0/24 dev radio0 proto kernel scope link src 192.168.200.2

192.168.232.0/21 dev wlan0 proto kernel scope link src 192.168.232.2

I do not see any of the 10.2.x.x ip address. Where are those ip address?

Also does the virtual router run within the emulator (on the Android OS) or run on windows? How to exam the virtual router?

回答1:

I have the same issue running Android 7.1.1 (API 25) on Windows. But when I emulating Android 7.0 (API 24) it works as expected. Device gets IP 10.0.2.15. In this case ip route show will bring 10.2.x.x address. So, if you can just use prior versions (API 16 - API 24).

This router exists inside emulator and Windows is not aware about it. If you need to access service running inside emulator from Windows, you need to add port mappings:

telnet localhost 5554
auth <token from 'C:\Users\{user}\.emulator_console_auth_token'>
redir add tcp:8080:8080

Then you can access your service by: 127.0.0.1:8080. However, this approach does not work with Android 7.1.1. I have no ideas what is wrong with it.



回答2:

It appears that since API 25, the networking of an emulator has changed. The device IP is no longer 10.0.2.15, and issuing a 'redir' command via telnet, as it is documented, no longer work.

In order to have a redirection, use the adb program. First, make sure adb is in your earch path. Typically, it is under Android/Sdk/platform-tools, wherever the Android SDK is located, whatever OS you are using.

Then, use the following command line:

adb forward tcp:<host port> tcp:<guest port>

(For example: adb forward tcp:8080 tcp:8080). Now, you can access port with: telnet localhost <host port>, which will be redirected to the guest.

This is very unfortunate, because (as of build 28) it isn't documented this way. I assume at some point documentation will be fixed.