Go how to send a broadcast packet

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The transmission of network packets is divided into three ways, namely unicast , multicast and 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, take a look at the broadcast-related knowledge:

We all know that the role of IP address is to locate a network interface in the network (yes, the IP is not to locate a host). The types of IP addresses we usually see are the following:

XXX.XXX.XXX.XXX

This type of IP address is called dotted decimal notation, in fact, the IP is a 32-bit long binary number, in each 8-bit units, divided into 4 parts, that 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 the computer sends a packet at the head of the packet plus the sender's IP address, and the recipient's IP address. However, when a typical computer sends a packet, all hosts in the same network receive the packet, but only that computer with the same IP address as the recipient in the packet will actually receive the packet.

If you want to allow all computers in the network (the same network) to receive this packet, you should set the recipient address of the packet to the highest host number in the network. Usually 255.255.255.255 can achieve this requirement. So if we want 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 execute the program, you can catch our broadcast packet sent:




If reproduced please specify the source: Http://blog.csdn.net/gophers




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.