'Searching' for games over a LAN network

Blitz3D Forums/Blitz3D Programming/'Searching' for games over a LAN network

Serpent(Posted 2009) [#1]
Hi everyone.

Basically, the question I'm asking is can you 'search' for multiplayer games over a LAN network.

The only way I currently know of doing this effectively is by using the 'StartNetGame' dialog, but I've been wondering if there is any way to bypass it. The DirectPlay dialogue searches the LAN network for any running DirectPlay games.

There are functions that allow you to host a DirectPlay game, or join one (if you know the IP address), but there is nothing that allows you to find other games without knowing the IP addresses of the hosts.

I have managed to write a short UDP program which finds the current computer's IP address, and just sends messages out to the 255 IPs that can replace the final byte. It listens for responses, and actually works. However, I was wondering if there is a better way (probably using DirectPlay) to search the network. As far as I know, more than the last byte of an IP address can vary from computer to computer (within the LAN network), and it would be impractical to search through more than 65000 IP addresses.

There should be some easier or at least faster way of doing this as the DirectPlay StartNetGame dialog demonstrates. I was wondering mainly if there was any way to use an inbuilt DirectPlay function to search the network - as I have seen this used in games like Age Of Empires II.

Any help or suggestions would be great.


Rroff(Posted 2009) [#2]
Generally unless its a very large network then games will be in the same IP range within a few numbers using either 192.168.0.x, 192.168.1.x or 10.0.0.x... pretty sure this is what directplay and most games do - just scan the 254 IPs either side of the last set of digits in your IP.

LAN IPs tho typically are:

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

Which is a massive range and its possible to have bridged networks, etc.


Wayne(Posted 2009) [#3]
Sub-net broadcast for class C network.
Example:
Send your packet to (192.168.1.255)


semar(Posted 2009) [#4]
http://www.blitzbasic.com/codearcs/codearcs.php?code=2592


Serpent(Posted 2009) [#5]
Thanks. Haven't heard of broadcasting before - very useful.


xlsior(Posted 2009) [#6]
The broadcast to x.x.x.255 only works if you are in a class C ( /24) network segment -- if you're in a smaller segment you need to use a different one.

To find out the proper broadcast IP you'll need to know both your own IP address AND your netmask, with those two you can calculate the broadcast IP. I'm not aware of any way to determine the netmask from within Blitz, though... :-?


Serpent(Posted 2009) [#7]
Windows definitely knows the network's subnet mask either way (e.g. 'ipconfig' tells you) - is there some API call or etc. that you can make?


Serpent(Posted 2009) [#8]
Okay - I have found a way to 'find' the broadcast IP without knowing the subnet mask.

It's messy, but it still works extremely fast and from my knowledge should work in all situations.

From my research, the subnet mask indicates the number of bits on the end of the IP address that will change.

For example, the subnet mask '255.255.255.0' would be written in bits as:
11111111 11111111 11111111 00000000

For all of the machines in the subnet, a different code is assigned to them using the 8 bits on the end.



To find the broadcast IP, you need to know your IP address and the subnet mask. In my case, my computer's IP address is 10.1.1.4, and the subnet mask is 255.255.255.0

'Wikipedia' describes finding the broadcast IP address as conducting a logical OR between the computer's IP address and the complement of the subnet mask - i.e. replace all of the bits that change (as identified by the subnet mask with 0s) in the IP address with 1s.

E.g. In my case, I would replace the last 8 bits of my IP address with 1s because that is the amount that changes, leaving me with a broadcast IP address of 10.1.1.255


Searching, I have seen possible ways of calculating the subnet mask from an IP address, but all seem to only work if the subnet mask is left as the default for the network class.

I realised that you don't need to know the exact subnet mask if there are a limited number of possibilities. Basically, in order to find another computer hosting a networked game, the program sends a message out to all possible broadcast IPs. It finds the binary form of the computer's IP address then, in a for-next loop replaces each bit from right to left, one by one, with a '1', and sends a message to the integer form of that IP.

For example, if the subnet mask (in binary form) was:
11111111 11111111 11111111 11110000

And my IP address was (in binary form):
10011101 00000001 00000001 11000011

The program would systematically go through:
10011101 00000001 00000001 11000011
10011101 00000001 00000001 11000011
10011101 00000001 00000001 11000111
10011101 00000001 00000001 11001111
10011101 00000001 00000001 11011111
etc until:
11111111 11111111 11111111 11111111

One of these will be the broadcast IP because - well, a subnet mask consists of a certain number of bits.

This method is admittedly slightly messy, but is simple when you understand it and, most importantly, should work no matter what the subnet mask is.


Sorry if my explanation is messy - but the idea is that the program systematically searches through all possible broadcast IPs to ensure that it at one point uses the correct one. Despite this being a messy approach, it executes in practically no time at all and works.