1
\$\begingroup\$

After sucessfully creating my first online multiplayer with servers and clients in UDP, I am wondering how LAN connections works. As it is right now, I need to open a server and forward the ports for the server if public. In LAN, they connection is possible but requires the local IP address.

However, if we take Minecraft or Starcraft, the game is able to auto detects servers on your local network.

How are they achieving this ?

\$\endgroup\$
  • \$\begingroup\$ Not sure what you mean by "need to forward ports". Your application/game should be simply operating on a specific port, I don't know why you would need to forward it. And LAN games work basically the same way, one client simply acts as a server and broadcasts it over the network via a predetermined port. \$\endgroup\$ – UnholySheep Sep 30 '16 at 18:49
  • \$\begingroup\$ When you set up a server (like a Minecraft, Terraria or one of my game) you have to configure the router for it to redirect the port on your machine. But in LAN you don't have to do that but I don't know how. Also how to detect actives games on a LAN. Oh I'm dumb it's just working without it. I will edit my question... \$\endgroup\$ – Winter Sep 30 '16 at 20:03
  • \$\begingroup\$ A router is a different story and completely unrelated to the game itself. You need to forward the port from the router, which is where anyone from outside your LAN will connect to, to your server, which only has a local IP. If your server were connected directly to the internet (without a router in between) no port forwarding would be necessary. And it used to be that players had to manually type in the IP address of another player they wanted to connect to (e.g.: In Warcraft 2 before Battle.Net or when playing LAN) (cont.) \$\endgroup\$ – UnholySheep Sep 30 '16 at 20:15
  • \$\begingroup\$ That being said a very simple way of detecting a local server would be to "ping" all possible IP addresses on the defined port your game uses and see which ones respond. \$\endgroup\$ – UnholySheep Sep 30 '16 at 20:17
  • \$\begingroup\$ An even simpler approach might be to have your server broadcast periodically and the potential clients just receiving a message containing the server's information \$\endgroup\$ – UnholySheep Sep 30 '16 at 20:31
1
\$\begingroup\$

To achieve this on a LAN you send a packet to the broadcast address.

For example if the network mask is 255.255.255.0 and the IP 192.168.1.100 you can send an UDP packet to 192.168.1.255 (ip | ~mask) and every computer on the LAN listening to the specified port should receive that packet.

Some rare network configurations don't let broadcast packets through, nothing you can do other than scan every possible IP addresses if that is the case.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.