Sign in to follow this  
AcidZombie24

scanning lan for servers

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
You need to know your server port, or a range. Then you can scan a range of ports, but that's not recommended.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by bencelot
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.
Can you give an example? It sounds to me like the game would be giving the user the option to specify a port; and all clients would need to connect to that port. In that case the port is still known, it's just that the user has to say what the port is.

There's no problem in using a fixed port; that's what the majority of games out there do. That makes tech support easier among other things (You know what port they should have open on their router). The only possible problem would be if the user already has an application running on the same port, but that's pretty unlikely (You have a 1 in ~60,000 chance), and you can just give a user friendly error if that happens (bind() will fail with WSAEADDRINUSE or something).
So long as you don't use a reserved port number (<= 1024), there shouldn't be a problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this