开发者

How to broadcast a message in a network?

I'm working on a client-server application written in C. I w开发者_如何学Pythonant to broadcast a message to all the machines available on the local network.

How can I do that using the usual socket system calls in C?


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.


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.


Have a look at UDP sockets.

I recomend Beej's Guide to Network Programming. Have a look at 6.3 Datagram Sockets


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜