UDP broadcast for computer discovery and server setup

I am working on a small network game prototype that will play on local networks using UDP. To detect other computers on the network, I am investigating broadcasting. However, I am still not sure of some details regarding the configuration / use of the UDP socket (new to the network). I found a good library to use after starting the game, but first all the computers on which the game is running should be detected, and one should be selected as the server. Therefore, I ask the following questions:

  • Is it possible to use one UDP socket to listen and send broadcasts? I am sure that the answer to this question is yes, but I wanted to check it.
  • When using UDP do you really need to use bind() ? As far as I understand, connect() not required, as well as send()/recv() , since they are TCP sendto()/recvfrom() ).
+7
source share
1 answer
  • Yes, you can send broadcasts, send unicast and receive packets (either broadcast or unicast) from one socket. This is VERY useful for the "Reply to Sender" work.

  • Not every socket should use bind. If you do not, the port will be selected automatically. But someone needs to bind a pre-shared port number so that the first packet (possibly broadcast) is correctly delivered. The first packet contains the source port and IP address; response packets can simply use this.

  • Associating both ends with fixed port numbers makes simplifying the firewall configuration.

  • setsockopt(SO_BROADCAST) , otherwise you will get errors trying to send broadcast packets.

+4
source

All Articles