Loading...

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

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

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.

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?

Posted on 11-01-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Chat App

    Chat App

    View: 67    Download: 1   Comment: 0

    Category: Android     Fields: none

    This is a Chat App for Android mobile. You can make separate chat with you partner. I'm using Mysql Database and Php code and All APIs are develop on PHP (REST APIs). My whatsApp:+918285672453: Email ID: nirbhayiise@gmail.com

  • Tower Defense 3D

    Tower Defense 3D

    View: 49    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    The goal in this game is to protect your territory against the attacking enemies. Place defensive structures on or along their path of attack to prevent them reaching your base. Strategic choice and positioning of defensive elements is an essential...

  • Flap the Bird

    Flap the Bird

    View: 34    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tap your screen to make the bird fly. While you are flying there are destruction that will stop you. Avoid hitting the floor and flying to high this will deduct your life. You can collect coins and life along the way. For more mobile games and...

  • Bad Santa

    Bad Santa

    View: 42    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This is a simple shooting game where the Bad Santa is shooting the bad snowman and reindeer. You can collect coins and life along the way. For more mobile games and tutorials you can visit this site.

  • Flappy Halloween Game

    Flappy Halloween Game

    View: 37    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Flappy Halloween is a simple game, tap the screen to move the player. Shoot the pumpkin in the ring to gain score. Avoid hitting the flying destructions and falling from the ground. Since the Halloween is coming this game is really perfect to play...

  • Tilt Ball Game

    Tilt Ball Game

    View: 34    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    This game is simple you need to tilt your device to make the ball move in your own desired direction. Reach the finish line before the time ends and move to the next level.

  • Tanks Dawn of Steel Game

    Tanks Dawn of Steel Game

    View: 63    Download: 0   Comment: 0

    Category: Android     Fields: Game - Entertainment

    Tanks Dawn of Steel is a shooting game which you have to defend yourself against incoming enemies for as long as you can. For every enemy that you destroy, you will receive a coin as a score. This game will challenge your skills in aiming and...

  • An Experience about Using Webview in Android Studio 3.0

    An Experience about Using Webview in Android Studio 3.0

    View: 132    Download: 0   Comment: 0

    Category: Android     Fields: Other

    I am blogging on ngocminhtran.com (using WordPress platform). Like most websites, I want to write an Android application to help viewers have a lot of great experiences with my blog.

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