Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need SocketOptions.SO_BROADCAST to enable broadcast?

Tags:

sockets

If we want to broadcast information from a socket, we need to enable SocketOptions.SO_BROADCAST. However, I don't understand why that is necessary.

My understanding is we set the packet with an broadcast address, just the same way as set a unicast address. Then we just need to send it through a regular socket. If its a UDP socket, then a UDP header will be added to that packet, and then an IP header containing the receiver's IP address (in this case is the broadcast address in the form of 192.168.255.255), and then a MAC address (FF:FF:FF:FF) is added.

I think the router will get the packet and perform the broadcast. I don't understand why we need to set the socket attribute to SO_BROADCAST.

like image 956
henryyao Avatar asked Apr 25 '13 14:04

henryyao


People also ask

What is So_broadcast?

The state of the SO_BROADCAST socket option determines whether broadcast messages can be transmitted over a datagram socket. This socket option applies only to datagram sockets. To set the state of this socket option, a WSK application calls the WskControlSocket function with the following parameters. Parameter. Value.

What is SO_ linger socket option?

The purpose of the SO_LINGER option is to control how the socket is shut down when the function close(2) is called. This option applies only to connection-oriented protocols such as TCP. The default behavior of the kernel is to allow the close(2) function to return immediately to the caller.

What is socket option?

In addition to binding a socket to a local address or connecting it to a destination address, application programs need a method to control the socket. For example, when using protocols that use time out and retransmission, the application program may want to obtain or set the time-out parameters.

What is So_bindaddr?

SO_BINDADDR. Fetch the local address binding of a socket (this option cannot be "set" only "gotten", since sockets are bound at creation time, and so the locally bound address cannot be changed).


1 Answers

"Socket semantics require that an application set the SO_BROADCAST option on before attempting to send a datagram to a base or broadcast address. This protects the application from accidentally sending a datagram to many systems."

Source

like image 167
Jeremy Friesner Avatar answered Oct 26 '22 23:10

Jeremy Friesner