scanning lan for servers

Started by
9 comments, last by Evil Steve 15 years, 6 months ago
How should my client find/scan my server on my LAN? they are all using a fixed/known port. The PCs IP address are dynamic. Usually in the 192.168.0.[100-125] range. How should i find the IPs of the active servers on my lan? i am curious how windows SMB does it too. The servers are TCP and blocking.
<SkilletAudio> Your framerate proves your lack of manhood
Advertisement
I can't provide all the details, but I believe IP multicast is the way to go. Host sends out an "I'm here" packet every 500ms, clients look for these packets. Or actually, the other way around would be better.

You can take a look at how Zeroconf works.
Yep, the other way around is how I just did it.

Have the client send out a broadcast packet to search for servers. i.e, a packet sent to ip 255.255.255.255

I flag this packet as being a broadcast packet (an int or something in the message header - I use enums for marking the message type such as broadcast packet, player details, disconnect request etc)

Have the server just listening on the network like normal, and if it receives a packet with the special broadcast marker on it, respond as required :) (eg send session info, login request etc)

Makes more sense to me to have the clients sending the broadcast messages rather than the server sitting there constantly sending them out on the network.
Multicast on the local LAN is not the way to go, because then you still need everyone to join the same multicast group. Instead, you want to use UDP broadcast.
Note that the Forum FAQ has a pretty good set of pointers for how to do this, including pointers to open source libraries.
enum Bool { True, False, FileNotFound };
I'm having a similar problem.

What if you don't know the port that the server might be hosting on?

I'm using enet, and I can find servers if I know the port (1234) by using:

ENetAddress address;
if(LAN) {
address.host = ENET_HOST_BROADCAST;
}
address.port = 1234;



but if I don't know the port... it won't work. Does anyone know if something like ENET_PORT_BROADCAST exists that I could use?

--------------------------Check out my free, top-down multiplayer shooter: Subvein
You need to know your server port, or a range. Then you can scan a range of ports, but that's not recommended.

Everything is better with Metal.

The simplest mechanism in brief:

The server listens for broadcasts on its given port.
The client, when starting to look for sessions, broadcasts a packet on that port.
The server, when it gets a broadcast (not multicast) packet, will reply to the requestor with information about itself.
enum Bool { True, False, FileNotFound };
Ok, but that requires knowing the specific port.

And I guess that I could scan a range, as you suggested, oliii. The range couldn't be too big though, or else it'd slow things down drastically.

So what do most games do? Restrict you to just 1 port? Or a range? And if so, how large is that range usually?

--------------------------Check out my free, top-down multiplayer shooter: Subvein
Quote:Original post by bencelot
So what do most games do? Restrict you to just 1 port? Or a range? And if so, how large is that range usually?
Yes, restrict you to one port. If you're running a server, you should always use a known port for exactly this reason.
That'd make sense. But why do I always see games giving the option to host on whatever port they want? What are the benefits?

Is it because some servers can't use the default port for whatever reason.. and have to use another one?

I could easily hardcode a default in.. but I'd rather not if it's going to limit the users.


Or, I suppose I could restrict it to a default only when someone wants to play LAN.. as when they play online, the game will be able to send their ip and port to a central server, which clients can download and know where to look.
--------------------------Check out my free, top-down multiplayer shooter: Subvein

This topic is closed to new replies.

Advertisement