Reaching a network device by IP and port using the Android emulator

See here:

Host machine can be reached using IP address from the emulator.

**edit, answer to your comment:*

For completeness and to better understand my answer, read the Android Emulator documentation.

These are the IP addresses as reached from the emulator:

  •, Router/gateway address.
  •, Special alias to your host loopback interface (i.e., on your development machine)
  •, First DNS server
  • / /, Optional second, third and fourth DNS server (if any)
  •, The emulated device's own network/ethernet interface
  •, The emulated device's own loopback interface

That said, we have:

  • Common mistake 1: accessing from the emulator trying to reach your host machine. Use, as I said.
  • Common mistake 2: Trying to access an emulator service on HostComputerIP:appServicePort. It won't work since your host computer itself (Windows, Linux, OS etc.) is not running a service in that port. You need to redirect a port on the emulator console to a port on an emulated Android instance itself (see 2 below).

Common networking needs:

1- Emulator app as client and local computer as server

Because the emulator is NAT'd, I believe you can connect to any computer on your local network directly. I mean, since the virtual router has access to both networks, it should be able to handle outgoing (i.e., emulator->real lan) connections just fine.

Example: on my network (192.168.0.x), I can connect from the emulator to my real router ( just pointing the emulator web browser to I use different services on it (hail to Tomato!), and I can access all of them on each port. No need to handle port forwarding, as expected.

By the looks of your code, I believe you need:

// I assume is your server, which is
// located on your local network, running a server application
// on port 9999.
cSocket = new Socket("",9999);

2- Local computer as client and emulator app as server

Now that's a different story. You need to setup port redirections on the virtual router. The easiest way is:

Telnet into the "management" system (this is not the emulator), from your host (your computer, console on linux or command prompt on Windows):

telnet localhost 5554

After that, use:

adb forward tcp:localPort tcp:emulatorPort

After this, you will be able to have a service on emulatorPort and you will be able to connect to it from computers in the local network by accessing hostComputerIP:localPort.

This is the way people (including me) use, for example, SSHDroid inside an emulator.

Anything else? goes to localhost. While that is your own computer from your PC, it's the phone from the phone. You need to provide the actual IP address. Additionally, if you haven't already set it, make sure you request the Internet permission. See the doc on permissions and this other SO post.

Update: To answer your comment, as David points out from the emulator you can use to reach your host machine.