Gemini14
Gemini14

Reputation: 6254

UDP broadcasting for LAN computer discovery & server setup

I'm working on a small, networked game prototype which will be played on LANs using UDP. For the discovery of other computers on the network, I've been investigating broadcasting. However, I'm still unsure about a few details regarding UDP socket setup/usage (networking newbie). I found a good library to use after the game is started, but at first, all computers running the game must be discovered and one has to be chosen as a server. So my questions are the following:

Upvotes: 8

Views: 4192

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283883

  1. Yes, you can send broadcasts, send unicasts and receive packets (either broadcast or unicast) all from a single socket. This is VERY useful for making "reply to sender" work.

  2. Not every socket needs to use bind. If you don't, a port will be chosen for you automatically. But someone has to bind a pre-shared port number in order for the first packet (possibly a broadcast) to be properly delivered. The first packet contains the source port and IP address; reply packets can just use this.

  3. Binding both ends to fixed port numbers does however make firewall configuration simpler.

  4. setsockopt(SO_BROADCAST), otherwise you'll get errors trying to send broadcast packets.

Upvotes: 5

Related Questions