How to connect a client on your host to a server running on Andoid emulator.

View: 24    Dowload: 0   Comment: 0   Post by: admin   Category: Android   Fields: none

9 point/1 review File has been tested

Introduction

Rising market for mobile apps requires rapid development in this field of application. To be more fast, emulators 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 the 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 most comfortable situation.

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

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

TcpListener listener = new TcpListener(4545);

On the client side we have a opposite line of code:

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

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

The question is now, how the emulator device can be reached at all? The must be also a simple port connection for it. And there is really one here. Take a look at 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 listen 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 follwing 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 biest 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 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, the is no redir-command available. May be it is a part of avd-subcommand? Who knows...

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

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

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

Simply make a follfing 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 see like this one:

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 a last screenshoot demostrates a working prototype:

Points of Interest

Rapid development of high quality first class application.

How to connect a client on your host to a server running on Andoid emulator.

How to connect a client on your host to a server running on Andoid emulator. Posted on 11-01-2018  Rising market for mobile apps requires rapid development in this field of application. To be more fast, emulators 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? 9/10 24

Comment:

To comment you must be logged in members.

Files with category

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd