Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I forward my localhost IP-Address to an Android Emulator?

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.

like image 907
ThaMe90 Avatar asked Feb 21 '11 09:02

ThaMe90


People also ask

Can I access localhost from 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.

What is localhost port forwarding?

Local port forwarding is the most common type of port forwarding. It is used to let a user connect from the local computer to another server, i.e. forward data securely from another client application running on the same computer as a Secure Shell (SSH) client.

Can you port forward on Android?

Port forwarding enables your Android device to access content that's being hosted on your development machine's web server. Port forwarding works by creating a listening TCP port on your Android device that maps to a TCP port on your development machine.


2 Answers

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...

like image 105
ThaMe90 Avatar answered Oct 12 '22 00:10

ThaMe90


You access your computer's localhost as 10.0.2.2 from emulator. more details here

like image 41
Aman Alam Avatar answered Oct 12 '22 02:10

Aman Alam