CompanyCodeMonkey
CompanyCodeMonkey

Reputation: 69

Socket communication on Android localhost

I'm developing multiple applications which need to communicate with each other via socket. First I need them to be able to communicate on localhost. So when I'm running both of them at the same time on my phone they can communicate (I know that there are easier ways to do this on the same phone but in the future they will run on separate phones).

My code for socket communication is very similar to this: link

Difference is that my apps are running this as foreground services.

I've set the ip for the server on the client to 127.0.0.1 but they just won't connect (not in the emulator and not on real phone). What am I missing?

UPDATE: I've found an easy way to get the device own IP address, so instead of localhost I use this (with www.google.com domain): answer to "java InetAddress.getLocalHost(); returns 127.0.0.1 … how to get REAL IP?"

Maybe it's not too nice but it works.

Upvotes: 0

Views: 1336

Answers (1)

nKn
nKn

Reputation: 13761

You'll need to set the local IP address of each one, so they can communicate within your LAN. So use an address like 192.168.1.X. Both if you're running your devices as virtual or physical, you may easily know the local IP address they have accessing your router's web interface and seeing their bound IPs.

Upvotes: 1

Related Questions