Click here to Skip to main content
15,868,164 members
Articles / Mobile Apps / Android
Tip/Trick

How to Connect a Client on Your Host to a Server Running on Andoid Emulator

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
28 Dec 2017CPOL3 min read 13.8K   3  
How to establish a connection to server running in Android emulator
The tip shows how to configure port forwarding to enable an Android emulator acting as a server to receive information from other local apps or remote clients, facilitating rapid development of mobile applications.

Introduction

Rising market for mobile apps requires rapid development in this field of application. To be more fast, emulators are used in a whole cycle of application development. Most of the time, app on device/emulator is running as client and connects to server on a host machine. But what if we need a vice versa?

Background

The typical scenario where app is client running on Android emulator will connect to server on a host machine is widely discussed. The client app simply use "10.0.2.2" instead of "localhost" for Android emulators or "10.0.3.2" for genymotion.

Using the Code

But there are some scenarios, like security or data transmission expansion, where the mobile app should act as server in order to receive information from other local app or remote client. In such a situation, we typically debug our app like server and use local client on a host machine - that is the most comfortable situation.

Assume we already have our client/server scenario developed in C#, and use port number 4545.

So, see the following line of code on the server (android app) part:

C#
TcpListener listener = new TcpListener(4545);

On the client side, we have an opposite line of code:

C#
TcpClient client = new TcpClient("localhost", 4545);

In this situation, the app running on a device emulator binds to a loopback interface that is local to an emulated device, and this is not the same loopback as "localhost" on the entire host machine...

The question is now, how can the emulator device be reached at all? There must also be a simple port connection for it. And there is really one here. Take a look at the upper-right side of your emulated device. Do you see?

Where is emulator port.

So, we see 5554 as our connection port. This is really good news to know, that Android emulator listens on some port. We can simply check this using an old good telnet. On most Windows installations, it must be first activated from Windows-Feature list:

After it is activated, you can simply try it from commandline (CMD) by using the following command:

telnet localhost 5554

And now you should get some response like this one:

Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
....
OK

That all looks fine and now you get an idea what we can use. That is an old known best with name port forwarding known as port redirection. The typical POSIX command will be "redir add ..." But stop. The bad news is, there is not here. Take a closer look typing "help" and press enter. You will see the following answer:

help
Android console commands:
    help|h|?
    help-verbose
    ping
    avd
    auth
    quit|exit

Try 'help-verbose' for more description
Try 'help <command>' for command-specific help
OK

Sadly, there is no redir-command available. Maybe, it is a part of avd-subcommand? Who knows...

The good news again, on the other side is that we know Visual Studio provides access to ADB-Tool commandline directly from the main UI over "Tools->Android->Android ADB commandline".

We also have excellent information about that tool on Android Studio site: https://developer.android.com/studio/command-line/adb.html#forwardports

Just assume, we want to use 7777 - this is our free choice - as our redirecting port number and 4545 as server listening port number.

Simply make a following call to ADB from ADB-Commandline:

adb forward tcp:7777 tcp:4545

And just change a port number on client site from real number 4545 to redirected port 7777. This should now look like this one:

C#
TcpClient client = new TcpClient("localhost", 7777);

That's All Folks!

The change of the port number from 4545 to 7777 on a client side is only the change we need in a whole code. Now compile, run and enjoy!

Here is the last screenshot that demostrates a working prototype:

Points of Interest

Rapid development of high quality first class application.

History

  • 28th December, 2017 - First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --