Because broadcasts and multicast do not establish a connection, only the UDP protocol is used.
Broadcasting is sending broadcast messages to all hosts in the LAN.
? 1, the representation of the IPV4 address {Subnet ID, host ID}a/b/c/d/e class address, 1 means all fields with bit 1
2. Subnet broadcast address, {subnet Id,-1}, specify the broadcast address of all interfaces on the subnet 192.168.1.0/24 the broadcast address on the subnet 192.168.1.255
3. Restricted broadcast address { -1,-1}255.255.255.255
int Sockfd=socket (af_inet, SOCK_DGRAM, 0);
struct sockaddr_in servaddr;
Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (13); /* Daytime server *///13 can be defined by itself
Inet_pton (Af_inet, "192.168.42.255", &servaddr.sin_addr);//broadcast address can be defined by itself
int on=1;
SetSockOpt (SOCKFD, Sol_socket, So_broadcast, &on, sizeof (on));//Turn on the radio
int len=0;
SendTo (SOCKFD, Sendline, strlen (Sendline), 0, servaddr, sizeof (SERVADDR));//Send, SERVADDR is broadcast address
Char recvline[maxline+1];//plus 1 is for null.
Socklen_t Len;
struct SOCKADDR *preply_addr;
Recvfrom (SOCKFD, Recvline, MAXLINE, 0,preply_addr, &len);//preply_addr returns the address of the sending data host, Len is the size of the address.
Multicast is the sending of messages to hosts of the same address group as their host.
1. Multicast IP Address
224.0.0.0 ~ 239.255.255.255
Multicast Ethernet address (MAC address)
Starting with a height of three bytes always 01:00:5e
2. Multicast is divided into two parts:
1, multicast data stream, the destination MAC address is 01:00:5e:xx:xx:xx
224.25.25.25 's MAC address is 01:00:5e:19:19:19
2. Multicast control flow (IGMP v1,v2,v3)
Three types of messages: report (join), Leave,query
This blog is written in very detailed, you can refer to http://blog.chinaunix.net/uid-22832715-id-2111502.html
Typically broadcast and multicast are used to discover the service side of the LAN.
Broadcast and multicast