An IP address is used to identify a host in the network. The IPV4 protocol represents a network address with a 32-bit unsigned number, including the network number and host number. The subnet mask indicates that the network number in the IP address occupies a few bytes.
Each network segment has a corresponding broadcast address. Take Class C segment 192.168.1.x as an example, where the smallest address 192.168.1.0 represents the network segment, and the largest address 192.168.1.255 is the broadcast address in the network segment. When we want this address to send a packet, all the hosts of that segment will receive and process it.
Broadcast packets are sent and received via UDP sockets
The broadcast packet sending process is as follows:
(1) Creating a UDP socket socket (af_inet, SOCK_DGRAM, 0)
(2) filling the broadcast information structure; struct SOCKADDR_IN
(3) Set socket option allows sending broadcast packets; setsockopt (,, So_broadcast,,)
(4) sending a packet; SendTo ()
Send.c
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <sys/types.h>5#include <sys/socket.h>6#include <arpa/inet.h>7#include <netinet/inch.h>8#include <string.h>9 Ten #defineErr_log (log) do{perror (log); exit (1);} while (0) One A #defineN 128 - - intMainintargcConst Char*argv[]) the { - - intsockfd; - structsockaddr_in broadcastaddr; + CharBuf[n] = {0}; - + if(SOCKFD = socket (af_inet, SOCK_DGRAM,0)) <0) A { atErr_log ("fail to socket"); - } - -broadcastaddr.sin_family =af_inet; -BROADCASTADDR.SIN_ADDR.S_ADDR = inet_addr ("192.168.1.255"); -Broadcastaddr.sin_port = htons (10000); in - intOptval =1; to + if(SetSockOpt (SOCKFD, Sol_socket, So_broadcast, &optval,sizeof(int)) <0) - { theErr_log ("fail to setsockopt"); * } $ Panax Notoginseng while(1) - { theprintf"Input >"); + fgets (buf, N, stdin); A if(SendTo (Sockfd,buf, N,0, (structSOCKADDR *) &broadcastaddr,sizeof(BROADCASTADDR)) <0) the { +Err_log ("fail to SendTo"); - } $ $ } - - return 0; the}
The broadcast packet reception process is as follows:
(1) Creating a UDP socket socket (af_inet, SOCK_DGRAM, 0)
(2) filling the broadcast information structure; struct SOCKADDR_IN
(3) Bind address and port; Bind ()
(4) receiving data packets; Recvfrom ()
Recv.c
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <sys/types.h>5#include <sys/socket.h>6#include <arpa/inet.h>7#include <netinet/inch.h>8#include <string.h>9 Ten #defineErr_log (log) do{perror (log); exit (1);} while (0) One #defineN 128 A - intMainintargcConst Char*argv[]) - { the - intsockfd; - CharBuf[n]; - structsockaddr_in broadcastaddr, srcaddr; + - if(SOCKFD = socket (af_inet, SOCK_DGRAM,0)) <0) + { AErr_log ("fail to socket"); at } - -broadcastaddr.sin_family =af_inet; -BROADCASTADDR.SIN_ADDR.S_ADDR = inet_addr ("192.168.1.255"); -Broadcastaddr.sin_port = htons (10000); - in if(Bind (SOCKFD, (structsockaddr*) &broadcastaddr,sizeof(BROADCASTADDR)) <0) - { toErr_log ("fail to bind"); + } - thesocklen_t Addrlen =sizeof(structsockaddr); * $ while(1)Panax Notoginseng { - if(Recvfrom (Sockfd,buf, N,0, (structSOCKADDR *) &srcaddr, &addrlen) <0) the { +Err_log ("fail to SendTo"); A } theprintf"buf:%s--->%s%d\n", buf, Inet_ntoa (SRCADDR.SIN_ADDR), Ntohs (Srcaddr.sin_port)); + } - $ return 0; $}
Broadcast of Linux network communication