Advertisement

List all servers over LAN (TCP/IP)

Started by January 27, 2018 02:48 PM
6 comments, last by swiftcoder 6 years, 9 months ago

So imagine i have 2 servers created on port X, one has ip1 another some other ip,

Now i would like to somehow find them, so client could list all of 2.

You know something like in quake3, where you just had a list of lan servers you could connect to.

There is an address called the broadcast address.  On IPv4 it is your address OR'ed with the subnet mask. In IPv6 it is the somewhat more convenient special address ff02::1.

The server listens to the game's port as normal. A game client broadcasts their connection attempt across the network, and the servers connect as they would a regular direct connection.

Advertisement

There are Three ways of doing this.

The first, which works over the Internet, is to have a third server, a listing server, with a known DNS name/port, and have each server register itself with the listing server. The listing server can then do NAT introduction / punch-through as appropriate.

The second, which works only on a local network, is to use UDP broadcast on a known port. Typically, an available server will send a broadcast "I'm here" every 10 second or so. A client that wants to find a game, will send a "who's there" broadcast, which will cause the servers to immediately respond with their "I'm here" messages, to speed up discovery. (You don't want to send "I'm here" every second, or even more often, because that generates a lot of pointless network traffic.)

The third is to make each client find out its own network address (which there is no good portable way of doing, so platform specifics are needed) and then probe the entire subnet, trying to connect to a known port on each address in the subnet, and for the nodes that respond, send some kind of packet that the servers will send a known response to. I recommend against this method, as some networks have probing defense systems in place that will isolate the node trying to do the probe, plus it's slower to determine which nodes don't actually have servers, because of time-outs (even if you can run probes in parallel.)

enum Bool { True, False, FileNotFound };

I dont get it, it seems I must provide some base address from which I start then iterate like 192.168.0.1 then 192.168.0.2 ?

 

saving server list on external server is not an option here.

If you are on subnet 192.168.0.x, then you can send a UDP broadcast to 192.168.0.255 to some port, and any node on the same network that listens to that port, for UDP datagrams, and has turned on SO_BROADCAST, will receive the broadcast message.

A shortcut is to send to the "mars" broadcast address of 255.255.255.255. This will be filtered by any router, but any computer that's on the same physical subset will see the broadcast. That way, you don't have to discover your own interface address, which there is no portable way to do.

You cannot do this only with TCP, you need to build in UDP listening.

 

enum Bool { True, False, FileNotFound };

If you use 255.255.255.255 it will broadcast on all interfaces so you don't have to figure out which one is the right one.
(Pair this with listening on 0.0.0.0 so you receive from all interfaces.)

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement

For LAN there are standard approaches to peer discovery. It may be easiest just to use an off the shelf implementation of zeroconf/Bonjour/mDNS/Avahi (confusingly, all the same thing). You've probably used this if you've ever connected Mac or iOS devices on a local network.

I've used it in the past to connect remote control apps running on android to hosts running Linux/MacOS on the same wifi network. It's pretty seamless, and very simple to setup.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement