组播接收端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#define MSG_ERR(msg) do{\
printf("%d\n",__LINE__);\
perror(msg);}\
while(0)
#define PORT 3333
#define IP "192.168.8.10"
#define GRP_IP "224.1.1.1"
int main(int argc, const char *argv[])
{
//创建套接字文件
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
MSG_ERR("socket");
return -1;
}
//加入多波组
struct ip_mreqn mem;
mem.imr_multiaddr.s_addr=inet_addr(GRP_IP);
mem.imr_address.s_addr=inet_addr(IP);
mem.imr_ifindex=2;
socklen_t len=sizeof(mem);
setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mem,len);
printf("加入成功\n");
//绑定Ip和端口号
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(GRP_IP);
socklen_t addrlen=sizeof(sin);
if(bind(sfd,(struct sockaddr *)&sin,addrlen)<0)
{
MSG_ERR("bind");
}
printf("绑定成功\n");
while(1)
{
//接收数据
struct sockaddr_in cin;
socklen_t addrlen_cin=sizeof(cin);
char buf[128];
bzero(buf,sizeof(buf));
ssize_t rcv;
rcv=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen_cin);
if(rcv<0)
{
MSG_ERR("recv");
return -1;
}
if(rcv==0)
{
printf("对方客户端关闭\n");
break;
}
printf("客户端ip=%s,port=%d,接受的数据为%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
close(sfd);
return 0;
}
组播发送端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#define MSG_ERR(msg) do{\
printf("%d\n",__LINE__);\
perror(msg);}\
while(0)
#define GRP_PORT 3333
#define GRP_IP "224.1.1.1"
int main(int argc, const char *argv[])
{
//创建套接字文件
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
MSG_ERR("socket");
return -1;
}
//接收端的端口和IP
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(GRP_PORT);
sin.sin_addr.s_addr=inet_addr(GRP_IP);
socklen_t addrlen=sizeof(sin);
while(1)
{
//发送数据
char buf[128];
bzero(buf,sizeof(buf));
printf("请输入向接收端发送的数据");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen)<0)
{
MSG_ERR("sendto");
}
printf("发送成功\n");
}
close(sfd);
return 0;
}
广播接收端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#define MSG_ERR(msg) do{\
printf("%d\n",__LINE__);\
perror(msg);}\
while(0)
#define PORT 8888
#define IP "192.168.8.255"
int main(int argc, const char *argv[])
{
//创建套接字文件
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
MSG_ERR("socket");
return -1;
}
//绑定广播Ip和端口号
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
socklen_t addrlen=sizeof(sin);
if(bind(sfd,(struct sockaddr *)&sin,addrlen)<0)
{
MSG_ERR("bind");
}
printf("绑定成功\n");
while(1)
{
//接收数据
struct sockaddr_in cin;
socklen_t addrlen_cin=sizeof(cin);
char buf[128];
bzero(buf,sizeof(buf));
ssize_t rcv;
rcv=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen_cin);
if(rcv<0)
{
MSG_ERR("recv");
return -1;
}
if(rcv==0)
{
printf("对方客户端关闭\n");
break;
}
printf("客户端ip=%s,port=%d,接受的数据为%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
close(sfd);
return 0;
}
广播发送端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#define MSG_ERR(msg) do{\
printf("%d\n",__LINE__);\
perror(msg);}\
while(0)
#define PORT 3333
#define IP "192.168.8.255"
int main(int argc, const char *argv[])
{
//创建数据报式管道文件
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
MSG_ERR("socket");
return -1;
}
//设置网络属性
int broad;
socklen_t len=sizeof(broad);
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,len)<0)
{
MSG_ERR("setsockopt");
return -1;
}
//广播的基本地址信息
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
socklen_t addrlen=sizeof(sin);
while(1)
{
//发送数据
char buf[128];
bzero(buf,sizeof(buf));
printf("请输入广播发送的数据");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen)<0)
{
MSG_ERR("sendto");
}
printf("发送成功\n");
}
close(sfd);
return 0;
}