Setup TCP and UDP forwarding to Android Emulator
Introduction
This is a note describing the steps needed to forward TCP and UDP traffic to the Android Emulator.
Sometimes when developing an application one need to connect and send data from the host machine to the app running in an Android Emulator. In order to do that there are some tricks needed for forwarding the traffic.
Forward TCP traffic to Android Emulator
Forwarding TCP traffic to android emulator is simple. Just use the adb
tool
like this to forward host port 2222 to port 3333 on the Android emulator:
$ adb forward tcp:2222 tcp:3333
Check with
$ adb forward --list
emulator-5554 tcp:2222 tcp:3333
Forward UDP traffic to Android Emulator
The forward
command in adb
tool can only forward TCP traffic. In order to
forward UDP traffic one need to telnet into the emulator itself and run a
command.
First you need the emulators auth token. The auth token can be found by doing
$ cat ~/.emulator_console_auth_token
When the emulator is running it opens a control port on port 5554, Connect to
the port using telnet
and use the command
auth <token>
to authenticate. Then use the redir
command to forward host UDP port 2222 to
emulator port 3333. This is the complete procedure:
$ telnet localhost 5554
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'/Users/bjorn/.emulator_console_auth_token'
OK
auth sU6**********XS8
Android Console: type 'help' for a list of commands
OK
redir
allows you to add, list and remove UDP and/or PORT redirection from the host to the device
as an example, 'redir tcp:5000:6000' will route any packet sent to the host's TCP port 5000
to TCP port 6000 of the emulated device
available sub-commands:
list list current redirections
add add new redirection
del remove existing redirection
redir add udp:2222:3333
OK
redir list
udp:2222 => 3333
OK
So as seen in the redir list
command host UDP port 2222 is now forwarded to
port 3333 on the emulator.