views:

96

answers:

4

Hi,

I'm working on a client-server application written in C, I want to broadcast a message to all the machines available in the network. How can I do that using the usual socket system calls in C ?

Thank you!

+1  A: 

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

For more info see section IP Network Broadcasting.

RC
+3  A: 

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

Enrico Carlesso
+1  A: 

Have a look at udp sockets.

I recomend beej guide, have a look at the 6.3 Datagram Sockets

stefanB
+2  A: 

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

then use the sendto() function call to send a datagram, and use 255.255.255.255 as the destination address. (for datagram sockets, you don't need to call connect(), since there is no 'connection').

in standard implementations, this address broadcasts to all computer in the local network, this means that the packet will not cross gateway boundaries and will not be received by computers using a network mask diferent from the network mask of the sending computer.

Adrien Plisson