0

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.

Community
  • 1
  • 1
  • 1
    "My code ... is very similar to ..." leads to lots of assumptions and often to problems missed completely => post **your** code. If the **only** "Difference is that my apps are running this as foreground services" than your code should be identical as you don't set the running mode in your code. In order to communicate with any devices over socket both sides need to know each other's IP addresses to connect. – Germann Arlington Mar 17 '14 at 14:33
  • The reason I didn't added my code is that my question doesn't really involves it since I know it should work (you can see on the link that it's not a big deal). I was only asking if there is a way to get the loopback IP or localhost for socket communication but there seems to be no easy way for this. – CompanyCodeMonkey Mar 17 '14 at 15:07

1 Answers1

1

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.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • Yeah but isn't there a universal solution? I mean I might have to present this on other network and I realy don't want to set the IP manually every time :( . – CompanyCodeMonkey Mar 17 '14 at 14:00
  • I'm afraid there's not. The only way you could achieve something like that is having your devices with both public and static IPs. Other way of achieving that (though I don't know if that fits to your implementation) is using Google Cloud Messaging, where you have a server that sends messages to all the devices that registers against your server. So this way you're not using IPs but you rely on the Google's list of devices that have registered. You have more info here: http://developer.android.com/google/gcm/index.html – nKn Mar 17 '14 at 14:15
  • Ok, thanks for the tip. I might get the current IP address via WifiManager or something like that. It does work if I manually add the IP tho, so you got an upvote :) . – CompanyCodeMonkey Mar 17 '14 at 15:03