目录
4、接收端怎么接收组播消息? -->需要加入组播属性的套接字
sendto("你好",192.168.14.255);
int socketfd = socket(AF_INET,SOCK_DGRAM,0);
- int on=1;
- setsockopt(sockfd , SOL_SOCKET,SO_BROADCAST,&on, sizeof(on));
- struct sockaddr_in sendAddr;
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = htons(10000);
- sendAddr.sin_addr.s_addr = inet_addr("192.168.14.255");//一定是广播地址,广播发送
- sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&sendAddr,sizeof(sendAddr));
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- #define GUANG_IP "192.168.11.255"
- #define GUANG_PORT 60001
-
- int main()
- {
- //建立套接字
- int socket_fd = socket(AF_INET,SOCK_DGRAM,0);
- if(socket_fd < 0)
- {
- perror("sokcet fail");
- return -1;
- }
-
-
- //设置广播属性
- int on=1;
- setsockopt(socket_fd , SOL_SOCKET,SO_BROADCAST,&on, sizeof(on));
-
- //给广播地址发送数据
- struct sockaddr_in send_addr;
- send_addr.sin_family = AF_INET;
- send_addr.sin_port = htons(GUANG_PORT);
- send_addr.sin_addr.s_addr = inet_addr(GUANG_IP);//广播地址
-
- char buf[1024] = {0};
- while(1)
- {
- bzero(buf,sizeof(buf));
- scanf("%s",buf);
- //发送的是实际的长度
- sendto(socket_fd,buf,strlen(buf),0,(struct sockaddr *)&send_addr,sizeof(send_addr));
- }
-
- //关闭套接字
- close(socket_fd);
- }
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- #define GUANG_IP "192.168.11.255"
- #define GUANG_PORT 60001
-
- int main()
- {
- //建立套接字
- int socket_fd = socket(AF_INET,SOCK_DGRAM,0);
- if(socket_fd < 0)
- {
- perror("sokcet fail");
- return -1;
- }
-
- //绑定广播地址
- struct sockaddr_in my_addr;
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(GUANG_PORT);
- my_addr.sin_addr.s_addr = htonl(INADDR_ANY);//INADDR_ANY本机任意IP地址--常用方法
- //send_addr.sin_addr.s_addr = inet_addr(GUANG_IP);//广播地址
- bind(socket_fd,(struct sockaddr *)&my_addr,sizeof(my_addr));
-
- char buf[1024] = {0};
- int ret;
- struct sockaddr_in recv_addr;
- socklen_t addrlen = sizeof(recv_addr);
- while(1)
- {
- bzero(buf,sizeof(buf));//清空缓冲区
- ret = recvfrom(socket_fd,buf,sizeof(buf),0,(struct sockaddr *)&recv_addr,&addrlen);
- char *ip = inet_ntoa(recv_addr.sin_addr);
- int port = ntohs(recv_addr.sin_port);
- printf("[ip:%s port:%d] buf:%s ret:%d\n",ip,port,buf,ret);
- }
-
- //关闭套接字
- close(socket_fd);
- }
- #define IP_ADD_MEMBERSHIP 加入组播
- // usr/include/linux/in.h
- struct ip_mreq {
- struct in_addr imr_multiaddr; /* 多播组的IP地址 224.0.0.10/
- struct in_addr imr_interface; /* 需要加入到多组的IP地址 192.168.53.134 */
- };
struct ip_mreq vmreq;
- inet_pton(AF_INET,"224.0.0.10",&vmreq.imr_multiaddr); // 组播地址
- inet_pton(AF_INET,"192.168.63.2",&vmreq.imr_interface); // 需要添加到组的ip
- #ininclude <arpa/inet.h>
- int inet_pton(int af, const char *src, void *dst);
setsockopt(socketfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&vmreq,sizeof(vmreq));
- struct sockaddr_in saddr;
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons(atoi(argv[1]));
- saddr.sin_addr.s_addr = htonl(INADDR_ANY); //htonl(INADDR_ANY) 代表 主机所有的地址
- bind(socketfd,(struct sockaddr *)&saddr,sizeof(saddr));
6、接收数据
recvfrom(......)
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
-
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- /*
- *发送端
- */
-
- #define GROUP_IP "224.0.0.10"
- #define GROUP_PORT 60002
-
- int main()
- {
- //建立套接字
- int socket_fd;
- socket_fd = socket(AF_INET,SOCK_DGRAM,0);
- if(socket_fd < 0)
- {
- perror("socket fail");
- return -1;
- }
-
- //往组播地址发送数据
- struct sockaddr_in send_addr;
- send_addr.sin_family = AF_INET;
- send_addr.sin_port = htons(GROUP_PORT);
- send_addr.sin_addr.s_addr = inet_addr(GROUP_IP);
- bind(socket_fd,(struct sockaddr *)&send_addr,sizeof(send_addr));
-
- //往组播地址发送数据
- char buf[1024] = {0};
- int ret;
- while(1)
- {
- bzero(buf,sizeof(buf));
- scanf("%s",buf);
-
- sendto(socket_fd,buf,strlen(buf),0,(struct sockaddr *)&send_addr,sizeof(send_addr));
- }
-
- //关闭套接字
-
- close(socket_fd);
- }
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
-
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- /*
- * 接收端
- */
-
- #define GROUP_IP "224.0.0.10"
- #define GROUP_PORT 60002
-
- int main()
- {
- //建立套接字
- int socket_fd;
- socket_fd = socket(AF_INET,SOCK_DGRAM,0);
- if(socket_fd < 0)
- {
- perror("socket fail");
- return -1;
- }
-
- //定义组播结构体
- struct ip_mreq vmreq;
-
- //添加组播地址
- inet_pton(AF_INET,"224.0.0.10",&vmreq.imr_multiaddr);
- //将自己的ip添加到到组播地址中
- inet_pton(AF_INET,"192.168.11.2",&vmreq.imr_interface);
-
- //设置组播属性到套接字中
- setsockopt(socket_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&vmreq,sizeof(vmreq));
-
- //绑定本机任意地址
- struct sockaddr_in saddr;
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons(60002);
- saddr.sin_addr.s_addr = htonl(INADDR_ANY); //htonl(INADDR_ANY) 代表 主机所有的地址
- bind(socket_fd,(struct sockaddr *)&saddr,sizeof(saddr));
-
- //接收数据
- struct sockaddr_in recv_addr;
- socklen_t addrlen = sizeof(recv_addr);
-
- char buf[1024] = {0};
- int ret;
- while(1)
- {
- memset(buf,0,sizeof(buf));
- ret = recvfrom(socket_fd,buf,sizeof(buf),0,(struct sockaddr *)&recv_addr,&addrlen);
- char *ip = inet_ntoa(recv_addr.sin_addr);
- int port = ntohs(recv_addr.sin_port);
- printf("[ip:%s port:%d] buf:%s ret:%d\n",ip,port,buf,ret);
- }
- //关闭套接字
- close(socket_fd);
- }