Talking TCP to a server on an Android emulator

You had a really slick idea. You wrote this TCP server that accepts socket connections and it runs on an Android device! You’ve been testing it all day and debugging it until it was bullet proof. You arrive home ready to test the last 4 liner modification you made just before leaving the office when you realize you left your device in the office! No worries, fire up the emulator and connect the test client on your mac to…? You test client expects an IP address and a port! Hi, I’m Cliff. You’re here because you left your Android phone on your desk underneath the 5oz bag of Lays BBQ chips. I’m here to give you some brief info on how to get your emulated Droid talking TCP  to its host environment via a telnet session and a redirect command.

The first thing you need to do is determine which port your emulator is bound to. The port number is usually listed in the title bar of the Window your emulator runs in. (Unless you’ve specified an override, the 1st emulator instance you start will bind to port 5554, and the next will bind to 5555 and so forth.)

Emulator Screen

Next, open your terminal (or “cmd” command prompt on Windows) and start a telnet session on localhost. This will connect you the emulator control console where you can enter port redirect commands. The command for redirect is:

redir add <protocol>:<host-port>:<guest-port>

So let’s say your socket server is running on port 8080 on the emulator and you want to redirect all traffic going to port 8081 on your Mac (or Windows PC), you would enter:

redir add tcp:8081:808

You can now “exit” the telnet session and from thenceforth all traffic hitting localhost:8081 will be forwarded directly to the emulator on its port 8080! When you are done with port forwarding you can connect to the emulator control console again via telnet and remove the port forward with the following command:

redir del tcp:8081

These comands work equally well with UDP. Use:

redir add udp:9001:9000

To forward UDP traffic from 9001 on localhost to the emulator’s port 9000 and use:

redir del udp:9001

To undo the change when you are done. There are various other things you can do with the emulator console. For more info see the Android developer docs on Using the Emulator Console.

One thought on “Talking TCP to a server on an Android emulator

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s