单播(unicast)
任播(anycast)
组播(multicast)
广播(broadcast)
注: 广播和组播都需要使用UDP,都不能使用TCP
地址表示方式:
-
IPv4地址的表示 {子网id:192.168.1, 主机id:12}
A/B/C/D/E五类地址,-1表示所有位都为1 的字段 -
子网广播地址,{子网id ,-1},指定子网所有接口的广播地址
192.168.177.0/24 该子网上的广播地址192.168.177.255 -
受限广播地地址 {-1 , -1}
255.255.255.255
单播UDP:
广播UDP:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <unistd.h>#include <fcntl.h>#include <arpa/inet.h>#include <time.h>#define MAX_LISTEN_QUE 5#define SERV_PORT 8888#define MAXLEN 200int main(){int sockfd;struct sockaddr_in server;struct sockaddr_in client;struct sockaddr_in broadcast;socklen_t addrlen,len;int num;time_t timep;int opt = 1;char read_buf[MAXLEN];char buf[MAXLEN] = \"hello.linux.\";//创建套接字if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){perror(\"Creatingsocket failed.\");exit(1);}//设置允许广播发送if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&opt,sizeof(opt)) < 0){perror(\"create error\\n\");return -1;}//初始化server结构体bzero(&server,sizeof(server));server.sin_family=AF_INET;//IPv4server.sin_port=htons(SERV_PORT);//端口号server.sin_addr.s_addr= htonl (INADDR_ANY);//设置为可以连接任何一个客户端//初始化broadcast结构体bzero(&broadcast,sizeof(broadcast));broadcast.sin_family=AF_INET;//IPv4broadcast.sin_port=htons(SERV_PORT);//端口号broadcast.sin_addr.s_addr= inet_addr (\"192.168.1.255\");//设置为可以连接任何一个客户端//将套接字和服务器的地址绑定if(bind(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1){perror(\"Bind()error.\");exit(1);}//得到存储客户端的结构体的长度addrlen=sizeof(client);//获得broadcast结构体的长度len = sizeof(struct sockaddr_in);//将数组中的内容发送到broadcast中num = sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&broadcast,len);if(num < 0){perror(\"sendto error\\n\");return -1;}while(1){//在recvfrom之前加,否则第一次读取发送方IP为0.0.0.0addrlen = sizeof(struct sockaddr);//从客户端读取数据,并存储在read_buf中num =recvfrom(sockfd,read_buf,MAXLEN,0,(struct sockaddr*)&client,&addrlen);if (num < 0){perror(\"recvfrom() error\\n\");exit(1);}//打印读取的内容以及ip地址和端口号printf(\"len:%d\\n\",num);printf(\"IP:%s,port:%d\\n\",inet_ntoa(client.sin_addr),ntohs(client.sin_port));printf(\"read_buf:%s\\n\",read_buf);//获取时间的函数timep = time(NULL);snprintf(buf, sizeof(buf), \"%s\", ctime(&timep));//发送数据到广播报sendto(sockfd,buf,strlen(buf),0,(struct sockaddr *)&broadcast,len);//清空客户端和数组bzero(&client, len);bzero(buf, MAXLEN);bzero(read_buf, MAXLEN);sleep(1);}//关闭套接字close(sockfd);return 0;}
运行结果
len:12IP:192.168.1.12,port:8888read_buf:hello.linux.len:25IP:192.168.1.12,port:8888read_buf:Sat Jul 25 15:10:25 2020len:25IP:192.168.1.12,port:8888read_buf:Sat Jul 25 15:10:26 2020
组播IP地址
224.0.0.0 ~ 239.255.255.255
组播以太网地址(MAC地址)
开头高三个字节总是 01:00:5e
组播分为两部分:
-
组播数据流 ,目的MAC地址为01:00:5e:xx:xx:xx
224.25.25.25 的mac地址为01:00:5e:19:19:19 -
组播控制流(IGMP v1,v2,v3)
分三种报文:report(join),leave,query
组播和广播的区别
广播:用户进程发送一组数据,经过数据链路之后通过网络发送给当前在线的所有主机。
组播:用户进程发送一组数据,经过数据链路之后通过网络发送在组播IP范围内的所有主机。
组播编程
server/client
client:1、IP_MULTICAST_LOOP(设置组播循环)
2、IP_ADD_MEMBERSHIP(添加进组播)
3、IP_DROP_MEMBERSHIP(从组播中移除)
server
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <unistd.h>#include <fcntl.h>#include <sys/epoll.h>#include <errno.h>#include <arpa/inet.h>#define MAX_LISTEN_QUE 5#define SERV_PORT 8888#define MAX_BUFFER_SIZE 100#define FD_MAXSIZE 1024#define MAX_EVENTS 10#define RT_ERR (-1)#define RT_OK 0#define MCAST_PORT 8888#define MCAST_ADDR \"224.25.25.25\"#define MCAST_DATA \"Hello.Client!\"#define MCAST_INTERVAL 3#define BUFF_SIZE 256int main(int argc, char*argv){int sockfd;struct sockaddr_in mcast_addr;char buf[100] = MCAST_DATA;int on = 1;int ret;//创建套接字sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (sockfd == -1){perror(\"socket()\");return -1;}//设置地址重用if((ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0){perror(\"Error, set socket reuse addr failed\");return -1;}//结构体mcast_addr的初始化memset(&mcast_addr, 0, sizeof(mcast_addr));mcast_addr.sin_family = AF_INET; //IPv4mcast_addr.sin_addr.s_addr = inet_addr(MCAST_ADDR);//绑定地址mcast_addr.sin_port = htons(MCAST_PORT); //端口号while(1) {//将buf中的数据发送到mcast_addr组播中int n = sendto(sockfd, buf, strlen(buf), 0,(struct sockaddr*)&mcast_addr,sizeof(mcast_addr));if( n < 0){perror(\"sendto:\");return -1;}//睡眠sleep(MCAST_INTERVAL);}return 0;}
client
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <unistd.h>#include <fcntl.h>#include <sys/epoll.h>#include <arpa/inet.h>#define MAX_LISTEN_QUE 5#define SERV_PORT 8888#define MAX_BUFFER_SIZE 100#define FD_MAXSIZE 1024#define MAX_EVENTS 10#define RT_ERR (-1)#define RT_OK 0#define MCAST_PORT 8888#define MCAST_ADDR \"224.25.25.25\"#define MCAST_DATA \"Hello.Server!\"#define MCAST_INTERVAL 3#define BUFF_SIZE 256int main(int argc, char*argv[]){int sockfd;struct sockaddr_in local_addr;int err = -1;int ret;int on = 1;int times = 0;int addr_len = 0;char buff[BUFF_SIZE];int n = 0;int loop = 1;//创建套接字sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (sockfd == -1){perror(\"socket()\");return -1;}//设置地址重用if((ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) < 0){perror(\"Error, set socket reuse addr failed\");return -1;}//结构体local_addr的初始化memset(&local_addr, 0, sizeof(local_addr));local_addr.sin_family = AF_INET;//IPv4local_addr.sin_addr.s_addr = htonl(INADDR_ANY);//任何一个客户端都可以连接local_addr.sin_port = htons(MCAST_PORT);//设置端口号//绑定local_addr到通信套接字中err = bind(sockfd,(struct sockaddr*)&local_addr, sizeof(local_addr)) ;if(err < 0){perror(\"bind()\");return -1;}//设置IP地址为循环err = setsockopt(sockfd,IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));if(err < 0){perror(\"setsockopt():IP_MULTICAST_LOOP\");return -1;}//创建IP结构体struct ip_mreq mreq;mreq.imr_multiaddr.s_addr = inet_addr(MCAST_ADDR); //设置要加入的组播的地址mreq.imr_interface.s_addr = htonl(INADDR_ANY); //本地接口的IP地址(设置为所有可连接)//添加一个sockfd加入组播到中err = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));if (err < 0){perror(\"setsockopt():IP_ADD_MEMBERSHIP\");return -1;}for(times = 0;times<5;times++){//获取结构体长度addr_len = sizeof(local_addr);//将数组清空memset(buff, 0, BUFF_SIZE);//读取套接字中的数据,保存到buff中n = recvfrom(sockfd, buff, BUFF_SIZE, 0,(struct sockaddr*)&local_addr,&addr_len);if(n == -1){perror(\"recvfrom()\");break;}printf(\"Recv %dst message from server:%s\\n\", times, buff);sleep(MCAST_INTERVAL);}//五次之后将sockfd从组播中移除err = setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP,&mreq, sizeof(mreq));close(sockfd);return 0;}
运行结果
Recv 0st message from server:Hello.Client!Recv 1st message from server:Hello.Client!Recv 2st message from server:Hello.Client!Recv 3st message from server:Hello.Client!Recv 4st message from server:Hello.Client!