Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access dev machine localhost via usb

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.

like image 353
a.bertucci Avatar asked Mar 26 '10 15:03

a.bertucci


1 Answers

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.

like image 195
a.bertucci Avatar answered Oct 01 '22 21:10

a.bertucci