Share the UDP broadcast address calculation method-Linux general technology-Linux technology and application information. The following is a detailed description. Winsock. h and winsock2.h both define several special addresses:
# Define INADDR_ANY 0x00000000
# Define INADDR_LOOPBACK 0x7F000001
# Define INADDR_BROADCAST 0 xFFFFFFFF
# Define INADDR_NONE 0 xFFFFFFFF
Don't see BROADCAST as a BROADCAST address. The broadcast address is calculated by the IP address and subnet mask (both of which are 4 bytes. The binary form of the subnet mask is high N-bit 1 and low (32-N) Bit 0. The IP address and subnet mask are bitwise AND operated to get the network number. The IP address with the same network number is considered to be in the same network segment.
For example, if the subnet mask 255.255.255.0, 192.168.1.2 and 192.168.1.3 belong to the same network segment, their network number is 192.168.1.0. But they and 192.168.0.4 do not belong to the same network segment, because the latter's network number is 192.168.0.0. However, for subnet mask 255.255.254.0, the three belong to the same network segment and their network number is 192.168.0.0.
After all the bits in the subnet mask are reversed, the broadcast address is used for the same or operation with the network number. Therefore, the relationship between broadcast address B and IP address I and subnet mask M is:
B = (I & M) | ~ M;
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