The principle and procedure of UDP receiving/sending broadcast packet

Source: Internet
Author: User
The principle and procedure of UDP receiving/sending broadcast packet if the applications on the two hosts on the network want to communicate with each other, one must know each other's IP, and the other is to know the port on which the program can listen. Because programs on the same host use the network to differentiate by port number. Use procedure for UDP sockets: 1. Initializes a network library of 2. Creates a socket of type SOCK_DGRAM. 3. Bind sockets. 4. Send and receive data. 5. Destroy sockets. 6. Release the network library. The principle of broadcast packets: one address that is specifically used to send to all workstations in the network at the same time is called a broadcast address. In a network using the TCP/IP protocol, the host identity segment host ID is a full 1 IP address as the broadcast address. If your IP is: 192.168.1.39, the subnet mask is: 255.255.255.0, the broadcast address is: 192.168.1.255; if IP is 192.168.1.39, the subnet mask is: 255.255.255.192, the broadcast address is: 192.168.1.63 。 If you only want to broadcast the data in this network, as long as you send a packet to the broadcast address, this packet can be routed, it will be through the router to all the hosts in the network segment, such broadcasts are also called direct broadcast; if you want to broadcast data across the network, To send a packet to the 255.255.255.255, the packet is not routed (not to the subnet), it can only reach all hosts in this physical network, which is called a limited broadcast. The process of sending and receiving broadcast packets using the UDP protocol. If we're going to send a broadcast packet to the 192.168.0.X subnet mask: 255.255.255.0 subnets. The steps are as follows: 1. Initializes the Winsock library. 2. Create a socket of type Sock_diram. 3. Set the properties of the socket to allow it to broadcast. 4. Send packets to 192.168.0.255 5. Receive a broadcast packet of your own broadcast. 6. Close Socket 7. Releases the network library. Note the following: 1. The receiver must know the broadcast's slogan, and then bind the port number to receive it correctly. 2. The receiver's socket does not need to be set to broadcast properties. 3. Bound IP can not use "127.0.0.1", you can use the real IP address or inaddr_any. Otherwise the receive fails.

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.