1、send.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
/*
创建一个组播聊天室,组播地址自己指定,任何人都可以加入组播组,有成员加
入的时候,广播发一条消息,加上自己的IP和端口。组播收发消息
退出组播地址的时候,广播发一条消息,加上自己的IP和端口。
*/
int main(int argc, const char *argv[])
{
if(argc!=2)
{
printf("请输入你的ip\n");
return -1;
}
//创建套接字
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//打开广播功能
int bc = 1;
socklen_t len = sizeof(len);
int bc_ret = setsockopt(sock_fd,SOL_SOCKET,SO_BROADCAST,&bc,len);
if(-1==bc_ret)
{
perror("set sockbroast fail");
return -1;
}
//组播地址 这是用来加入组播的
struct ip_mreq multi1;
multi1.imr_multiaddr.s_addr = inet_addr("224.10.10.11");
multi1.imr_interface.s_addr = inet_addr(argv[1]);//本机地址
socklen_t multi1_len = sizeof(multi1);
setsockopt(sock_fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&multi1,multi1_len);
//绑定地址 能发送广播消息
struct sockaddr_in broadcast;
broadcast.sin_family = AF_INET;
broadcast.sin_port = htons(10088);
broadcast.sin_addr.s_addr= inet_addr("192.168.124.255");
socklen_t broadcast_len = sizeof(broadcast);
//绑定地址 能在组播里发送消息
struct sockaddr_in multi;
multi.sin_family = AF_INET;
multi.sin_port = htons(10088);
multi.sin_addr.s_addr = inet_addr("224.10.10.11");
socklen_t multi_len = sizeof(multi);
bind(sock_fd,(struct sockaddr*)&multi,multi_len);
//向组播地址发送消息并进行广播
char *buf = malloc(100);
fgets(buf,100,stdin);
sendto(sock_fd,buf,100,0,(struct sockaddr*)&broadcast,broadcast_len);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
sendto(sock_fd,buf,100,0,(struct sockaddr*)&multi,multi_len);
if(strcmp(buf,"bye\n")==0)
{
sendto(sock_fd,buf,100,0,(struct sockaddr*)&broadcast,broadcast_len);
setsockopt(sock_fd,IPPROTO_IP,IP_DROP_MEMBERSHIP,&multi1,multi1_len);
break;
}
}
return 0;
}
2、recv.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
/*
创建一个组播聊天室,组播地址自己指定,任何人都可以加入组播组,有成员加
入的时候,广播发一条消息,加上自己的IP和端口。组播收发消息
退出组播地址的时候,广播发一条消息,加上自己的IP和端口。
*/
int main(int argc, const char *argv[])
{
//1 创建套接字
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//打开广播功能
int bc = 1;
socklen_t len = sizeof(len);
int bc_ret = setsockopt(sock_fd,SOL_SOCKET,SO_BROADCAST,&bc,len);
if(-1==bc_ret)
{
perror("set sockbroast fail");
return -1;
}
//绑定地址实现广播
struct sockaddr_in brodcast;
brodcast.sin_family = AF_INET;
brodcast.sin_port = htons(10088);
brodcast.sin_addr.s_addr = INADDR_ANY;
socklen_t brodcast_len = sizeof(brodcast);
//绑定地址实现组播
struct sockaddr_in local,opposite;
local.sin_family = AF_INET;
local.sin_port = htons(10088);
local.sin_addr.s_addr = INADDR_ANY;
socklen_t local_len = sizeof(local);
socklen_t opposite_len = sizeof(opposite);
bind(sock_fd,(struct sockaddr*)&local,local_len);
char *buf = malloc(100);
//收消息
while(1)
{
recvfrom(sock_fd,buf,100,0,(struct sockaddr*)&brodcast,&brodcast_len);
printf("[端口号:%d,ip:%s]:from brodcast:%s\n",ntohs(brodcast.sin_port),inet_ntoa(brodcast.sin_addr),buf);
memset(buf,0,100);
//接对端的消息
recvfrom(sock_fd,buf,100,0,(struct sockaddr*)&opposite,&opposite_len);
printf("from multi:%s\n",buf);
if(strcmp(buf,"bye\n")==0)
{
recvfrom(sock_fd,buf,100,0,(struct sockaddr*)&brodcast,&brodcast_len);
printf("[端口号:%d,ip:%s]:from brodcast:已退出\n",ntohs(brodcast.sin_port),inet_ntoa(brodcast.sin_addr));
}
}
return 0;
}