Search code examples
javajavafxnetwork-programmingtcpserver

How to discover devices on a network running your code


I am working on a Java application with which people can send data between devices on a network. Every device with the code installed is running a TCP server and is listening on a certain port. Now I would like the user to have the option to "scan" the network for other devices running my code, and display a list on the screen to select which device to send data to.

How can I find which devices are running my code?

I can only think of two ways:

  • trying to connect to every possible IP on a specific port, but that seems like it would take ages
  • Launching a UDP broadcast on a different port with your IP and have the others respond, but that would require running two servers and having two open ports

So i kindly ask you, is there a better way of achieving this?

Thank you.


Solution

  • The way I do this is to broadcast a periodic "heartbeat" via multicast and listen for that. Nowadays the best practice is to use multicast DNS using something like jmDNS (https://github.com/openhab/jmdns).