views:

424

answers:

2

My activity needs to communicate with a java server located on my dev machine. Actually, using the emulator, I can access the server socket via the special 10.0.2.2 address. Now I'd like to test my app on my phone connected via usb, but it seems like I can't use 10.0.2.2 to access my dev machine localhost anymore. Naturally using a wifi router and a proper lan addressing does the trick, but I need to use usb connection instead. Any hint ?

edit:
Just added different scenarios to let everyone understand better:

1. Emulator-based scenario:

  • standard java server running on my pc @localhost:15000
  • android java client running on my emulator.

When the android app needs to contact the server it uses 10.0.2.2:15000 like specified here. Everything works fine.

2. Proper wifi scenario with physical connected device:

  • standard java server running on my pc @192.168.1.10:15000.
  • android java client running on my device with 192.168.1.11 address assigned.

When the android app needs to contact the server it uses 192.168.1.10:15000 and everything works fine too.

3. Desired scenario:

  • standard java server running on my pc @localhost:15000.
  • android java client running on my device connected via usb cable. No network is active.

When the android app needs to contact the server it tries to connect to 10.0.2.2:15000 but instead an exception is thrown (Network unreachable). It seems like special address 10.0.2.2 works only for emulator instances.

I just wonder if in the adb/adt suite there's a way to access pc address from the device connected via usb cable. I hope it's a bit more clear now. Thanks.

A: 

You can setup a connect to your phone over USB from your dev pc, but you have to adb forward. However, I don't think you can route the otherway around over usb: setting up a connection from the phone to the pc.

You could have your pc connect to the phone and check which IP it has.

While thinking about it, I realized, using adb forward you have to use localhost as your host, so maybe you could also do this on your phone: adb forward the port you want to use and on your phone, connect to that port on localhost.

MrSnowflake
I don't use any port listening on my device. The server is on my dev machine instead. So, if IIRC, adb forward works the opposite way (forwarding ports number of "listening" sockets) and this is not the case.
mr_archano
Yeah, that's what I said, I wasn't sure connecting from your device to your pc would work. Sorry.
MrSnowflake
A: 

It seems like there's no way to let scenario #3 work properly, because usb connection is not a real network connection and there's no ip associated to it by the phone.

Actually my solution is to use an ad-hoc wifi connection between my phone and my pc. This scenario is similar to #2, but without router or dhcp. Unfortunately my Hero can't handle ad-hoc connections natively because of this issue. So I had to root my device and to change manually some config script. I hope functionality like this can be available in next releases.

mr_archano