2

For a small java game I made, I would like to be able to play it with two computers in the same (home) network. I think I will use RMI and am now trying with computer on ipaddress 192.168.2.3. I know I can search for a registry on this ipaddress on my other computer at 192.168.2.6, but I would like to show a list of all ipaddresses in the network my computer is connected with. Prefferably only if they actually host a game.

Now I tried some questions here on stackoverflow:

,but I don't think I need all my computer network interfaces and InetAddress.isReachable() always seem to result to false (even though I can ping via cmd and I have firewall turned off) and calling a commandline

"ping -n 1 192.168.2.i" for all i, where 0<=i<=255,

always exits normal, so it results always in true.

What is the best way to get a list of ipaddresses of computers in the same network as the computer the JVM runs on?

Community
  • 1
  • 1

1 Answers1

2

With the linked answers, you should be able to filter the available interfaces down to a few possible options (i.e. interfaces that are up, no loopback, have an IPv4 address, etc.).

To discover game hosts, you can do something like the following.

  • Let the game hosts listen for UDP broadcasts on a specific port.
  • Let the clients send out a UDP broadcast to the broadcast address of each of the remaining interfaces from above. The broadcast address can be determined by getBroadcast() in class InterfaceAddress.
  • The host replies, to let the client know it is waiting. When using UDP, the hosts IP is in the received DatagramPacket. When using TCP, the hosts IP can be determined from the Socket.
  • Then the client can use the address of the host to establish a direct connection and/or set up RMI.

Edit: I found this blog post, which includes code that does more or less what I described.

Prometheus
  • 1,005
  • 6
  • 16
  • I'm a bit confused. I thought the interfaces only give the current computer's ipaddress(es), but in your second bullet you imply that I can get the ipaddresses of other computers. Could you give an (link to) example which shows how I can get those other computer's ipaddresses? – Liontack Lightning Jan 30 '15 at 22:00
  • @LiontackLightning Not it doesn't. It says send to the *broadcast address* of the NIC. The NIC is still local. – user207421 Jan 30 '15 at 22:44
  • @LiontackLightning You can get the broadcast address from the local interfaces. A broadcast reaches all computers on the network. You don't need to know the specific destination address. I updated my post and included a link to an example. – Prometheus Jan 30 '15 at 23:01
  • @Prometheus The broadcast address I can connect through now always is '192.168.2.254', which is the router. But on one laptop the broadcast address is '0.0.0.0' and this laptop can't find any server. I can't find the fix for this laptop. Do you have suggestions for this? – Liontack Lightning Feb 01 '15 at 13:07
  • All I can say is that those aren't broadcast addresses. Were they returned by the getBroadcast() method? Hard to say what went wrong without the code. – Prometheus Feb 02 '15 at 22:47