user59330 user59330 - 1 year ago 77
Java Question

How to find Socket.IO server on the same LAN?

I developing an app that connects devices on the same network.

Any devices can be the server, I want clients to be able to find the server automatically without users having to enter the IP address of the server manually.

This is how I plan to do it:

  1. Find the IP of the client, eg

  2. Loop through 192.168.0.(0->255)

  3. Try to connect with all those IPs until the connection success

Is that the right way? Can I do it faster? Do IP on the same network always in the range of x.x.x.(0->255)?

I'm using both Java and JavaScript(Node.js) if that is relevance.

Answer Source

One option here: instead of "iterating" the address range and sending individual packets to each address; you could consider sending a broadcast to the whole subnet.

In other words: your client just shouts "I am here"; the server "hears" that and responds; similar to how protocols like DHCP work.

Edit on the comment on how to react to "broadcast not answered":

Actually, you are now coming closer to those topics that make "distributed" computing hard. There are many problems that could kick in; and many different solutions to them.

It starts with: do you go with one broadcast; or instead try multiple times?! And maybe increase the delays between subsequent broadcasts?

Thing is: nobody here can tell you that. The answers very much depend on your "domain", and what makes the most sense to the users of your application.

My suggestion here: look into existing open source products that do similar things; and study what kind of problems they identified; and how they deal with that. I know, this is pretty broad; but that "broadness" comes out of "the overall subject is really broad".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download