Connecting to Server with unknown IP-Address

779 views Asked by At

I have a Electron Client/Server Application where the Server Communicates with a couple of Clients over the LAN. This communication is established via node-ipc.

The Problem is, that I have no control over the LAN DHCP. So at startup I have no idea of the Server IP, and to make it worse it may be that the Clients are ready before the Server even is started.

So my Server script is:

const NodeIpc = require('node-ipc');
NodeIpc.config.id = 'world';
NodeIpc.config.retry = 1500;
NodeIpc.config.networkHost = FunctionToReadIP();

NodeIpc.serveNet(
    function () {
      whatever();
    });

And on the Client:

const NodeIPC = require('node-ipc');
NodeIPC.config.id = ClientID;
NodeIPC.config.retry= 1500;

NodeIPC.connectToNet(
'world',
function(){
    NodeIPC.of.world.on(
        'connect',
        function(){
            NodeIPC.of.world.emit('Data.fromClient',{data: ClientID});
        }
    );
  });

As long as I test everything on one Machine it works great, but I cannot connect other Machines. I there any way the Server and Client can find each other as soon as they are started, without knowing who has which IP?

2

There are 2 answers

1
Niklas Higi On

I haven't worked with node-ipc, but I'm pretty sure that for a client-server socket connection to be established, only the client needs to know the address of the server.

There are multiple solutions to this. You can ...

  • have the user enter the address of the server
  • make sure the server is always available under the same address using a domain
  • have another server that always has the same address tell the client which address the actual server is currently using
  • if the server is inside the user's local network, scan the network to find the server
  • if it's for a small, private project, use a service like ngrok to "tunnel" the port of the server
0
Torf On

I found a way to communicate with my Server by just using the Computer name (which I have access to).

So adding NodeIPC.config.networkHost = 'ServerName'; to my Client did the trick. That way I only have to set up the name of my Server properly and do not have to touch the IP-Address.