广播的接收端
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.204.255"
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
/*int broad=1;
setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad));*/
//填充信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
ssize_t res=0;
char buf[128]="";
//接受广播信息
while(1){
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,NULL,NULL);
if(res<0){
ERR_MSG("recvfrom\n");
return -1;
}
printf("%s\n",buf);
}
return 0;
}
广播的发送端
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.204.255"
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
int broad=1;
setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad));
//填充信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定
/*if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success\n");*/
ssize_t res=0;
char buf[128]="";
//发送广播信息
while(1){
bzero(buf,sizeof(buf));
scanf("%s",buf);
res=sendto(sfd,buf,sizeof(buf),0,\
(struct sockaddr*)&sin,sizeof(sin));
if(res<0){
ERR_MSG("sendto\n");
return -1;
}
}
return 0;
}
组播的接收端
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#define ERR(msg) do{\
fprintf(stderr,"%d:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define GRP_IP "225.1.1.1"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return -1;
}
//加入多播组
struct ip_mreqn cin;
cin.imr_multiaddr.s_addr=inet_addr(GRP_IP);
cin.imr_address.s_addr=inet_addr("192.168.204.130");
cin.imr_ifindex=if_nametoindex("ens33");
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&cin,sizeof(cin))<0){
ERR("setsockopt");
return -1;
}
printf("加入多播组成功\n");
//填充接收端ip和端口
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr(GRP_IP);
sin.sin_port=htons(PORT);
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR("bind");
return -1;
}
printf("bind success\n");
//接收数据
//socklen_t addrlen=sizeof(sin);
char buf[128]="";
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,NULL,NULL);
if(res<0){
ERR("recvfrom");
return -1;
}
printf("%s\n",buf);
}
close(sfd);
return 0;
}
组播发送端
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#define ERR(msg) do{\
fprintf(stderr,"%d:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define GRP_IP "225.1.1.1"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return -1;
}
//加入多播组
/*struct ip_mreqn cin;
cin.imr_multiaddr.s_addr=inet_addr(GRP_IP);
cin.imr_address.s_addr=inet_addr("192.168.204.130");
cin.imr_ifindex=if_nametoindex("ens33");
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&cin,sizeof(cin))<0){
ERR("setsockopt");
return -1;
}
printf("加入多播组成功\n");*/
//填充接收端ip和端口
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=inet_addr(GRP_IP);
sin.sin_port=htons(PORT);
//绑定
/*
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR("bind");
return -1;
}
printf("bind success\n");*/
//发收数据
socklen_t addrlen=sizeof(sin);
char buf[128]="";
ssize_t res;
while(1){
bzero(buf,sizeof(buf));
scanf("%s",buf);
res=sendto(sfd,buf,sizeof(buf),0,\
(struct sockaddr*)&sin,&addrlen);
if(res<0){
ERR("sendto");
return -1;
}
}
close(sfd);
return 0;
}