I need to connect an android device to my java program via socket connection. The device is working as server. The problem is that while trying out socket connection I need to give IP address and port number of server.
Is there something like a static IP address for an Android device to connect? If not, is there any alternative way to establish a socket connection between the device and my program?

- 3,984
- 1
- 32
- 62

- 59
- 1
- 8
-
1This is possible to a degree with caveats and concerns, generally only on local wifi or via the tethering or debug USB connection. A little searching here will find plenty of previous coverage. – Chris Stratton Mar 11 '15 at 16:05
1 Answers
As the device has to have a connection to the same network as the computer, it has to have an IP address configured. So you have to use that one. The used port is defined by the server application running on your device.
If you have the problem that anyhow a normal network connection is not possible, but you have an ADB connection, you can forward local pots to the device and let your server listen: http://developer.android.com/tools/help/adb.html#forwardports
Over a mobile connection (GPRS, EDGE, UMTS, HSDPA) there will be no practicable way to establish a connection from somewhere to your device (instead you have to do it the other way, while canceling the whole client-server directive), in cause of the used IP sharing. In that case you have a problem of which I'm not aware if it's possible in general, not to mention how you could achieve this.
Otherwise you simply have to configure a static IP for your device when connected to your local network, or you have to evaluate the actual IP of your device every most of the time, while using it with this configuration:

- 1
- 1

- 3,984
- 1
- 32
- 62
-
Thanks for the instant help provided. :-) I hav connected my device to the pc via usb. Now the problem is that i get an ip for the device only when i connect to internet, which i guess is a dynamic ip. So the only solution i hav will be to somehow read that dynamic ip via my code and use that for socket connection? – Vinay Mar 12 '15 at 07:12
-
What do you mean with "internet" your WLAN/LAN or a connection over mobile data connecions (GPRS, EDGE, UMTS, HSDPA)? Anyhow look at the updated answer ;) – Marvin Emil Brach Mar 12 '15 at 07:37
-
By internet i meant mobile data connections. But looks like it wont work. I tried hardcoding the static ip of my android device and tried establishing the socket connection. But still the connection failed. Looks like it might not be possible. Meanwhile i was able to connect to the emulator by giving the localhost ip (127.0.0.1). Thanks for the help provided Marvin :-) – Vinay Mar 12 '15 at 09:12