Network Libary see all servers in network

Hello,
I am working on a whiteboard, which can be seen and edited cross devices in the same network. It works perfectly, but now i found a new Problem: Currently i’m connecting to localhost (127.0.0.1), but for cross-device i need the IP-address of the currently available servers. I want to show them in a nice drop-down list, and the user can select it. Again, they are in the same WLAN/LAN and i want NOT the IP-address of my device, i stored it in a global variable.

EDIT: The devices are sending data in JSON-Format

Thank you for listening,
Lightosk

You want to discover the servers in the same network: great! Do all servers have the same functionality or each of them have a specific function? Do they have fixed ip addresses?

If they have fixed ips, you could ping those known addresses every so many seconds. However, you still need to have a “worker” to do this job. See below.

If the servers have the same functionality, you could connect to any of them. You might even consider an algorithms to connect randomly or in round-robin fashion to ensure load is distributed across all avail servers.

You could have a discovery service. How does it work? You will need to have always one server running, call it server 0 or root server. Every time a new server joins, it needs to register to this 0 server and you can always count on 0 server to provide the list of available servers. This is not enough. This 0 server needs to check the status of those registered servers say every 5 seconds. If any server fails the status check, say 3 times in a row, the server is removed from the list of available servers.

If the servers all have diff functionality then I find a small issue. How do I know what each server does? Listing a server by number is not terribly useful unless you know those ip numbers by heart and you know exactly what each server does. In other words, this would not be user friendly. In case you have a zero server, every time a server register, the server could add meta-data during the registration process like a name for the server and a description. Instead of listing IP addresses you will list the name. Alas, this sounds very close to a DNS configuration. Check on global resources how you can do this setup in a local network or check the details to see if this is a viable solution for your use case.

Just some ideas that I can think of…

Kf

It’s a bit complicated, because i want to run the app/server at any network. So it’s simply not possible to configure every local network on the world, and so are no fixed IPs possible. And yes, they are all doing the same: Hosting a whiteboard, but the whiteboards are different. Now i’m displaying on server side the server’s IP, and ask on client side for that IP. Maybe it’s possible to ask the router for the current connected device, i found java code here (Java code, sure translateable):

import java.net.InetAddress;
import java.net.Socket;

public class Main {
    public static void main(String[] args) {

        int timeout=500;
        int port = 1234;

        try {
            String currentIP = InetAddress.getLocalHost().toString();
            String subnet = getSubnet(currentIP);
            System.out.println("subnet: " + subnet);

            for (int i=1;i<254;i++){

                String host = subnet + i;
                System.out.println("Checking :" + host);

                if (InetAddress.getByName(host).isReachable(timeout)){
                    System.out.println(host + " is reachable");
                    try {
                        Socket connected = new Socket(subnet, port);
                    }
                    catch (Exception s) {
                        System.out.println(s);
                    }
                }
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
    }

My program is running on port 7270, so it should be possible to ask for this port without running in other programs, see here and here

Let’s say that you only have 3 servers but you will still explore all the IP range, with a timeout of 500 ms (I am guessing it is ms units here) it can take close to 128 seconds aka 2 min to explore all your available servers. These are 2 minutes that your client will not have this info. If servers has ephemeral ips, then how would you know its ip change? would you go ahead and check the whole ip space again, another 2 minutes? I am saying this is possible but not optimal.

This is confusing. Why to have multiple servers? Maybe you mean one server and multiple clients?
You can have a single server that can manage the data and it could build the dashboard based on a client id and request parameters. Even better, the dashboard will be a different web application and this application interacts with a server to populate the dashboard.

I imagine your application to be like a chat application. A client connect to a single server which provides a chatroom. In the chatroom you can add, edit and delete messages. If a client wants to connect to a different chat room, it will send the request to this single server to render the other chat room.

Going back to your OP, what other solutions have you seen on other online communities beside the solution you shared in the last post?

Kf

I’ve got multiple clients and servers. Why isn’t there just one server? The devices (one device can be a client or server) which i target (RPI Zero) have not much power, so will probably crash if they need to render multiple whiteboards.

I am not familiar with RPI Zero.

A single server is a suggestion based on the limited information you shared here.

Going back to your question: You want an user to discover all the servers in your local network. Would this client send InetAddress.getByName(host).isReachable(timeout) to each possible IP to see if it is accessible? Would each client do that?

In the thread you shared before, they also discusses the arp command. You should look into it and see if it applies to your case.

Kf

1 Like

I believe the point they are trying to make is that you have no way of knowing which of the network devices happen to be running your application and serving up a whiteboard. As far as your server code is concerned it’s serving something and the clients should magically know to talk to it. On the client side you presumably know the IP of the server for connecting manually.

To make the setup somewhat more seamless (e.g. plug and play) you must either implement some method within the client to discover what servers exist or provide a default server to connect to which will inform the client about other servers (which of course will have to talk to the primary one to register themselves).

In the latter case you can use a server with a statically configured IP address and configure all your ‘whiteboard servers’ to talk to it. That server would essentially function as a hub.


I don’t know much about how the above works, but that might be useful in this case. If you can get that working you simply need to assign names to the servers you’re using then tell people what the name of the server they should connect to is. In this model the client can ask for a server by name and the server named that will tell the client what it’s network address is.

You can as others have pointed, have the client blindly attempt to talk to every device to see if it’s a server, or vice versa with the server seeking client. But that is horribly inefficient and may look a lot like suspicious behavior to a network administrator, particularly if it is a non-standard port…

This approach may not be ideal if security is a major concern. If so you may want to try a different approach.

1 Like