Send network packets into three different ways, each unicast , multicast, broadcast .
Broadcast, in layman's words, is that the packets sent by your machine can be received by all the hosts in the same network.
Before explaining how to send a broadcast packet. Let's take a look at the radio-related knowledge:
We all know that the role of IP address is to locate a network interface in the network (yes, accurately speaking. IP is not locating a host).
The forms of IP addresses we usually see are such as the following:
XXX.XXX.XXX.XXX
The form of such an IP address is called dotted decimal notation, in fact the IP is a 32-bit long binary number, in every 8 bits, divided into 4 parts. It is divided into 4 bytes.
The IP address is composed by: [network number]+[host number]
The network number is used to identify a network, and the host number is used to identify a host in the network (in fact, an interface on the host).
Usually when a computer sends a packet, it adds the sender's IP address to the header of the packet. And the recipient's IP address.
However, when a computer sends a packet, all hosts in the same network receive the packet, but only the same computer that has the same IP address as the recipient's IP address in the packet will actually receive the packet.
Assume that you want all computers in the network (the same network) to receive this packet. The recipient address of this packet should be set to the highest host number in the network. Usually 255.255.255.255 can meet this requirement. So let's say we're going to send a UDP broadcast message, we can try the following example code:
Package Mainimport "NET" Func Main () {//Set the sender's IP address here, and look at their own IP settings laddr: = Net. UDPADDR{IP: net. IPV4 (192, 168, 137, 224), port:3000,}//here set the receiver's IP address to broadcast address RADDR: = Net. UDPADDR{IP: net. IPV4 (255, 255, 255, 255), port:3000,}conn, err: = Net. DIALUDP ("UDP", &LADDR, &RADDR) if err! = Nil {println (err. Error ()) Return}conn. Write ([]byte (' Hello Peers ')) Conn. Close ()}
Open Wireshark, set the filter rule to Udp.port = = 3000, and then run the program. We'll be able to catch the broadcast packet we sent:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz29wagvycw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
If reproduced please specify the source: Http://blog.csdn.net/gophers
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Go how to send a broadcast packet