2

My machine is running on window 7. I want to communicate between two android emulator's running on same machine.
Emulator first is listening on some IP 10.0.2.15 When second emulator tries to create

Socket socket = new Socket ("10.0.2.15",8080);

It gives exception, unable to connect to 10.0.2.15 does any one know how to solve this problem?

EDIT: I've already read https://developer.android.com/studio/run/emulator-networking#connecting but it says

On B's console, issue redir add tcp:8080:80

What does it mean by B's console, or where is B's console??

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
MAK
  • 575
  • 1
  • 10
  • 23
  • Older question, but there's a newer one that is very similar with a couple more answers. http://stackoverflow.com/questions/5118494/android-2-emulators-communicating –  Feb 28 '11 at 19:00

2 Answers2

5

What you need is to install TELNET on your Windows 7 machine. For that, Control Panel -> Programs and Features -> Turn Windows Features On or Off -> Telnet Client (must be ticked).

Then, in cmd (command prompt), you can say adb devices (if the Android SDK is on your PATH), which returns identifiers such as emulator-5554 and emulator-5556.

Now with telnet, you can access them with telnet localhost 5554 or telnet localhost 5556.

initial connection

To get them to tell you which emulator it is, you can type avd name.

But more importantly, it tells you this:

Android Console: Authentication required
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'C:\Users\[youruser]\.emulator_console_auth_token'
OK

Which is a text file that contains some random cryptic text.

You can copy paste that into the telnet like so:

auth cdPi82HewjZg

to which it will say OK, now you can actually run the command the documentation said.

after auth

Now you can say

redir add tcp:6000:4000

Which means: if the emulator would receive something to Port 6000 from LocalHost, then it should receive it as 4000

Which means your other emulator can connect to it through the 10.0.2.2 magic loopback IP by sending data to 6000, and it is the other emulator that will receive it, with port 4000.

no session yet

joined session

It also works not just for tcp: but also for udp:.

You can list redir and even remove redirections with redir del.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • @CommonsWare sorry about lashing out last night, it really was 3 AM (eventually 5) and I really did find only 5 different answers that said nothing beyond "read the docs". – EpicPandaForce Jun 10 '19 at 11:24
  • 1
    `¯\_(ツ)_/¯` -- after I deleted my admittedly crappy old answer, your opening material was no longer relevant. which is why I pruned it. Your answer is excellent! Plus, I'm impressed that you dredged up the old instructions for adding telnet to Windows 7. :-) – CommonsWare Jun 10 '19 at 11:35
  • what should be the port number passed into server and client socket , if use same tcp:6000:4000 – Chetan Joshi Dec 29 '20 at 12:28
  • The emulator that is redirected to `6000:4000` should be the server socket at `4000`, and the client socket should try to connect to it at `6000`. – EpicPandaForce Dec 30 '20 at 07:15
0

There may be two reasons

  1. As per my knowledge is concerned you have run is < 2.3 version
  2. Add Internet permission
  3. Try to run the server first say in emulator number 5554 and client (say) 5556
    then type

    telnet localhost 5554
    
gnat
  • 6,213
  • 108
  • 53
  • 73
arun
  • 1