Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP broadcasting for LAN computer discovery & server setup

Tags:

c++

c

udp

broadcast

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:

  • Can a single UDP socket be used to listen for and send broadcasts? I'm pretty sure the answer to this is yes, but I wanted to verify it.
  • When using UDP, do you actually have to use bind()? As far as I understand, connect() is not required, nor is send()/recv(), since they are for TCP (sendto()/recvfrom() being the replacements).
like image 207
Gemini14 Avatar asked Jun 18 '11 00:06

Gemini14


People also ask

What is UDP discovery?

If using UDP, the Biamp software discovers units on the network by broadcasting data packets to all IP addresses contained within your PC's subnet (i.e. the IP range that your PC is set in).

How do I enable UDP broadcast?

To configure and enable UDP broadcast forwarding on the switch: Enable routing. Globally enable UDP broadcast forwarding. On a per-VLAN basis, configure a forwarding address and UDP port type for each type of incoming UDP broadcast you want routed to other VLANs.

Is broadcast TCP or UDP?

TCP doesn't support Broadcasting. UDP supports Broadcasting. TCP is used by HTTP, HTTPs, FTP, SMTP and Telnet. UDP is used by DNS, DHCP, TFTP, SNMP, RIP, and VoIP.

Does UDP use IP address?

UDP uses IP to get a datagram from one computer to another. UDP works by gathering data in a UDP packet and adding its own header information to the packet. This data consists of the source and destination ports on which to communicate, the packet length and a checksum.


1 Answers

  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.

like image 161
Ben Voigt Avatar answered Sep 22 '22 09:09

Ben Voigt