[TCP/IP] Broadcast-Definition, Principle and Programming Implementation

        This article has a total of 2974 words and the estimated reading time is 4 minutes.

Table of contents

broadcast

Principles and forms of broadcasting

Programming and Implementation of Broadcasting

Socket option settings

sender

receiver

Expand information


broadcast

        Broadcast refers to a transmission method in which when packets are transmitted in a computer network, the destination address is all devices in the network. The "all devices" mentioned here are also limited to a range, which is called the "broadcast domain".

        Based on the characteristics of broadcast, broadcast is generally used in LAN but not in WAN.

Principles and forms of broadcasting

        The data transmission protocol of multicast is the same as multicast, which is completed through UDP protocol. At the same time, broadcasting is divided into two forms, namely:

  • Direct broadcast (broadcast that can span different networks)
  • Local broadcast (broadcast only within the local network)

        Direct broadcast is used to transmit data to a host in a specific area (the network address of the target host is known). In use, if the IP address and subnet mask of the target host are known to be 192.168.1.0/24 , then the broadcast address is 192.168.1.255 . (It should be noted that 255.255.255.255 is a restricted broadcast address and cannot be used). When sending a data packet according to this address, the router will send the data packet to all hosts under 192.168.1.1 ~ 192.168.1.254 , as shown in the figure below Show:

        Local broadcast is used for communication within the local network (can only be used under LAN). No matter what IP network a particular host is on, the current host can always use the address 255.255.255.255  to send packets to every node on the local network. For example, if any host in the 192.168.0 network segment sends a data packet to 255.255.255.255 , thenall hosts in the 192.168.0 network segment will receive the data packet, and at the same time, the data packet will not be forwarded to other network segments. As shown below:

expand:

        How to know which broadcast domain a host belongs to?

        You can know which broadcast domain the host belongs to by performing an AND operation on the host's IP address and subnet mask.

        For example: the IP address of a host is 192(1100 0000).168(1010 1000).23(0001 0111).150(1001 0110), and the subnet mask is 255(1111 1111).255.255.0, then it The broadcast domain it belongs to is (the actual calculation is in binary~):

192.168.23.150 \wedge 255.255.255.0 = 192.168.23.0

        Then all other hosts in the broadcast domain 192.168.23.0 can receive the broadcast packets sent by the device.

expand:

        How to calculate broadcast address?

        The broadcast address is calculated by "inverting" the subnet mask and then performing an "OR operation" with the broadcast domain.
        For example: the broadcast domain to which the host currently belongs is 192(1100 0000).168(1010 1000).0.0, and the subnet mask is 255.255.0.0, then the broadcast address is (is an OR \veeoperation, that is, |; \simis an inverse operation):

192.168.0.0 \vee (\sim255.255.0.0) = 192.168.255.255

Programming and Implementation of Broadcasting

Socket option settings

        The programming implementation of broadcast is similar to multicast, the difference lies in the setting of socket options. Through the setcokopt function, set the option level to  SOL_SOCKET  , and the corresponding option is  SO_BROADCAST  . The option is set to 1, which means "data broadcasting is possible".

int send_sock;
int so_brd = 1; //对变量进行初始化以将 SO_BROADCAST 选项信息设为 1 

send_sock = socket(PF_INET , SOCK_DGRAM , 0);

setsockopt(send_sock , SOL_SOCKET , SO_BROADCAST , (void*) & so_brd , sizeof(so_brd));

sender

        The implementation is similar to multicast, mainly focusing on the setting of socket options.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 1024

//报错消息发送
void Sender_message(char *message)
{
    puts(message);
    exit(1);
}


int main(int argc, char *argv[])
{
	int send_sock;
	struct sockaddr_in brd_addr;
	FILE *fp;
	char buf[BUF_SIZE];
	int so_brd=1;
	
	send_sock=socket(PF_INET, SOCK_DGRAM, 0);
    if(send_sock==-1)
    {
        Sender_message((char*)"socket creation error");
    }	
	memset(&brd_addr, 0, sizeof(brd_addr));
	brd_addr.sin_family=AF_INET;
	brd_addr.sin_addr.s_addr=inet_addr(argv[1]);
	brd_addr.sin_port=htons(atoi(argv[2]));
	
	setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST, (void*)&so_brd, sizeof(so_brd));	
	fp=fopen(argv[3], "r");
    if(fp==NULL)
    {
		Sender_message((char*)"file open error");
    }

	while(!feof(fp))
	{
		fgets(buf, BUF_SIZE, fp);
		sendto(send_sock, buf, strlen(buf), 0, (struct sockaddr*)&brd_addr, sizeof(brd_addr));
	}

	close(send_sock);
	return 0;
}

receiver

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUF_SIZE 1024

// 报错消息发送
void Sender_message(char *message)
{
    puts(message);
    exit(1);
}

int main(int argc, char *argv[])
{
    int recv_sock;
    struct sockaddr_in addr;
    int str_len;
    char buf[BUF_SIZE];

    recv_sock = socket(PF_INET, SOCK_DGRAM, 0);
    if (recv_sock == -1)
    {
        Sender_message((char *)"socket creation error");
    }
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(atoi(argv[1]));

    if (bind(recv_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        Sender_message((char *)"bind error");
    }
    while (1)
    {
        str_len = recvfrom(recv_sock, buf, BUF_SIZE - 1, 0, NULL, 0);
        if (str_len < 0)
        {
            break;
        }
        buf[str_len] = 0;
        fputs(buf, stdout);
    }

    close(recv_sock);
    return 0;
}

operation result:

Expand information

[1]  Detailed explanation of functions and parameters of setsockopt function - Blog Park 

[2]  What is the difference between local broadcast and targeted broadcast? - Know almost

[3]  Analysis and differences between multicast and broadcast principles - CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_42839065/article/details/131375292