How can I forward my localhost IP-Address to an Android Emulator?
Solution 1
To forward a port from your local machine to an Android Emulator, you need to have Telnet enabled. This is done through the Control Panel -> Programs and Features -> Turn Windows features on or off -> scroll down to Telnet Client. Then select it & press Ok. Now from your command prompt (with the Emulator running) you type telnet localhost "EmulatorPortNumber"
. The "EmulatorPortNumber" can be found in the titlebar of the Emulator, in my case it was 5554.
You will now see something similar to this:
Android Console: type 'help' for a list of commands
OK
Here, you want to type with as little as possible typo's, as when you apply the backspace, it will corrupt your command and not accept it. The command you have to use here is redir
. This will let you choose from 3 subcommands:
list list current directions
add add new redirection
del remove existing redirection
The one you need now is add. But you can't use it just like that. Typing redir add
will give you the following line:
KO: bad redirection format, try (tcp|udp):hostport:guestport
This means that you have to specify what kind of port you want to forward (TCP or UDP port), which port on the local machine you want to forward (hostport) & which port you want to set on the Emulator (guestport). So, using a command like this:
redir add udp:1337:12345
forwards the UDP port 1337 on the local machine to port 12345 on the Emulator. Be cautious about deleting redirections, as they might crash your Emulator. You can also simply close the Emulator to remove any redirections. It's easier & safer...
Solution 2
You access your computer's localhost
as 10.0.2.2
from emulator. more details here
Solution 3
You can use the adb forward command to set up arbitrary port forwarding — forwarding of requests on a specific host port to a different port on an emulator/device instance. Here's how you would set up forwarding of host port 6100 to emulator/device port 7100:
adb forward tcp:6100 tcp:7100
For example run NanoHTTPD web server on the AVD and then execute this command on the computer:
adb forward tcp:8080 tcp:8080
Open the browser on the computer and navigate the below address:
localhost:8080
http://developer.android.com/tools/help/adb.html#forwardports
Related videos on Youtube
ThaMe90
Hi, I have gained some experience with C/C++/C#, Java, HTML, XML, PHP, JavaScript and some other few languages. Nevertheless, you never stop learning, and this is a great site to get your knowledge tested and expanded. Anyways, lets see what happens.
Updated on July 01, 2022Comments
-
ThaMe90 almost 2 years
I know it is possible to forward a port from my develop machine to an Android emulator, but how is this done? I've found the solution on the android-developers site, but I couldn't see how they've meant their instructions... Anybody got some clear instructions on this? My develop machine is running Windows.
-
Aman Alam about 13 yearsyou mean something like '127.0.0.1:8080' ?? Did you try '10.0.2.2:8080' ?
-
ThaMe90 about 13 yearsNo, not exactly. What I meant was how I could forward a port on my local machine to the android emulator. However, I already found out how to do this...
-
Aman Alam about 13 yearsIf you did find out, please paste it here so that others searching for solution to this can also get helped.
-
Velidan about 4 yearsAfter you enter into Telnet shell to have access to the 'redir' command you need to authorize yourself via running "auth <emulator_auth_token>" command. The emulator token could be found in (Windows) the file: c:/users/<your_username>/.emulator_console_auth_token. Just open this file, copy the code and insert after the 'auth' command in the telnet terminal
-
Rohit Singh over 2 yearsThe url does not point to specific section. It is just old page with a ton of documentation for different topics.
-
Aman Alam about 2 years@RohitSingh If you noticed the URLs, they're different. Meaning the URL I put in my answer in 2011 is now dead, and DAC is now redirecting to their FAQs home page. A better URL would be: developer.android.com/studio/run/emulator-networking#connecting