How to broadcast a message on the network?

I am working on a client-server application written in C. I want to send a message to all the machines available on the local network.

How to do this using regular system socket calls in C?

+4
source share
4 answers

Just send a message to the broadcast address of your subnet, which for 192.168.0.0/24 is equal to 192.168.0.255 or simply transmitted to 255.255.255.255.

+3
source

you need to use UDP to send a broadcast message over the network. when creating a socket using the socket() function, specify AF_INET for the family parameter and SOCK_DGRAM for the type parameter. on some systems, you need to enable broadcast packet sending by setting the SO_BROADCAST socket option to 1 using setsockopt() .

then use the sendto() function call to send the datagram and use 255.255.255.255 as the destination address. (for datagram sockets, you do not need to call connect() , since there is no β€œconnection”).

in standard implementations, this address is transmitted to the entire computer on the local network, which means that the packet will not cross the gateway and will not be accepted by computers using a network mask that is different from the network mask of the sending computer.

+6
source

Look at udp sockets.

I recommend the beej guide , look at 6.3 Datagram Sockets

+2
source

You can use the special address 255.255.255.255 to send a broadcast message to each computer on the local network.

See IP Broadcasting for details.

+1
source

Source: https://habr.com/ru/post/1302588/


All Articles