0

I have a server running on an Android emulator, it's listening on localhost:SERVER_PORT (let's say 6789 in this example). I also have a client app on my emulator that sends a string to localhost:SERVER_PORT (localhost:6789) and in return gets another string.

Now I was wondering how I could access the server from the host (a Mac). I read up on https://developer.android.com/studio/run/emulator-networking and tried the following

$ nc localhost 5554
@ redir add tcp:5000:6789
@ exit
$ nc localhost 5000 (nothing happens)
$ nc localhost 6789 (nothing happens)

where EMULATOR_PORT = 5554, SERVER_PORT = 6789, HOST_MAPPED_PORT = 5000.

I also tried running ifconfig locally on the mac, but I got way too many interfaces and didn't know which one to try (I have lo0, en0, p2p0, awdl0, en1, en2, bridge0, utun0,1,2).

So how exactly can I achieve this? The expected behavior (I think) would be:

$ nc some_ip server_port
@ type_in_a_string
@ get_a_string_from_server
Xzenon
  • 1,193
  • 2
  • 15
  • 37
  • Use 10.0.2.2 instead of localhost – Sahil Manchanda May 20 '18 at 19:00
  • If you bind the server to localhost, you will only be able to access it from them server device. You have to bind or to the wildcard address (could be 0, or 0.0.0.0) in order to listen on all interfaces or to the interface that is accesible to the network where you Mac is. Probably the ip assigned by your wifi router. – Juan May 20 '18 at 19:02
  • @SahilManchanda I tried to run `nc 10.0.2.2 6789` and it does connect to something, but it seems it's not the server (it doesn't log anything); or did you mean I should use the 10.0.2.2 for something else? – Xzenon May 20 '18 at 19:12
  • have a look at this question https://stackoverflow.com/q/9808560/2299040 – Sahil Manchanda May 20 '18 at 19:15
  • @SahilManchanda The answer is basically copy/pasted from the link I provided. I still am not sure how I should proceed. – Xzenon May 20 '18 at 19:23
  • 1
    You want to connect your workstation to the server running on your Android Emulator if i am getting that right then you need to run this line in terminal adb forward tcp: 6789 tcp: 6789 then you can access the server from your workstation – Sahil Manchanda May 20 '18 at 19:31

0 Answers0