广播服务器
在这#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
//#include <stdio.h>
#define ERR(msg) do{\
fprintf(stderr,"%d",__LINE__);\
perror("msg");\
}while(0);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return 0;
}
//绑定
struct sockaddr_in md;
md.sin_family=AF_INET;
md.sin_port=htons(8888);
md.sin_addr.s_addr=inet_addr("192.168.43.255");
int asd=bind(sfd,(struct sockaddr*)&md,sizeof(md));
if(asd<0){
ERR("bind");
return 0;
}
//接收
char c[128]="";
struct sockaddr_in sd;
ssize_t rad;
socklen_t arrlen=sizeof(sd);
while(1){
bzero(c,sizeof(c));
rad=recvfrom(sfd,c,sizeof(c),0,(struct sockaddr*)&sd,&arrlen);
if(rad<0){
ERR("recvfrom");
return 0;
}else if(0==rad){
printf("对端关闭\n");
break;
}
printf("[%s:%d] %s\n",inet_ntoa(sd.sin_addr),ntohs(sd.sin_port),c);
}
close(sfd);
return 0;
}
里插入代码片
广播客户端
在这#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
//#include <stdio.h>
#define ERR(msg) do{\
fprintf(stderr,"%d",__LINE__);\
perror("msg");\
}while(0);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return 0;
}
//允许广播
int num=1;
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&num,sizeof(num))<0){
ERR("setsockcpt");
return 0;
}
struct sockaddr_in md;
md.sin_family=AF_INET;
md.sin_port=htons(8888);
md.sin_addr.s_addr=inet_addr("192.168.43.255");
char c[128]="";
ssize_t rad;
socklen_t arrlen=sizeof(md);
while(1){
printf("请输入>>>");
bzero(c,sizeof(c));
//发送
fgets(c,sizeof(c),stdin);
c[strlen(c)-1]='\0';
rad=sendto(sfd,c,sizeof(c),0,(struct sockaddr*)&md,arrlen);
if(rad<0){
ERR("recvfrom");
return 0;
}else if(0==rad){
printf("对端关闭\n");
break;
}
}
close(sfd);
return 0;
}
里插入代码片
组播服务器
在这#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <net/if.h>
#define ERR(msg) do{\
fprintf(stderr,"%d",__LINE__);\
perror("msg");\
}while(0);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return 0;
}
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERR("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
struct ip_mreqn sp;
sp.imr_multiaddr.s_addr=inet_addr("224.6.6.6");
sp.imr_address.s_addr=inet_addr("192.168.43.9");
sp.imr_ifindex=if_nametoindex("ens33");
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&sp,sizeof(sp))<0){
ERR("setsockopt");
return 0;
}
struct sockaddr_in md;
md.sin_family=AF_INET;
md.sin_port=htons(8888);
md.sin_addr.s_addr=inet_addr("224.6.6.6");
if(bind(sfd,(struct sockaddr*)&md,sizeof(md))<0){
ERR("bind");
return 0;
}
char c[128]="";
ssize_t rad;
socklen_t arrlen=sizeof(md);
while(1){
//接收
bzero(c,sizeof(c));
rad=recvfrom(sfd,c,sizeof(c),0,(struct sockaddr*)&md,&arrlen);
if(rad<0){
ERR("recvfrom");
return 0;
}else if(0==rad){
printf("对端关闭\n");
break;
}
printf("%s\n",c);
}
close(sfd);
return 0;
}
里插入代码片
组播客户端
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
//#include <stdio.h>
#define ERR(msg) do{\
fprintf(stderr,"%d",__LINE__);\
perror("msg");\
}while(0);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR("socket");
return 0;
}
//绑定
struct sockaddr_in md;
md.sin_family=AF_INET;
md.sin_port=htons(8888);
md.sin_addr.s_addr=inet_addr("224.6.6.6");
//接收
char c[128]="";
struct sockaddr_in sd;
ssize_t rad;
socklen_t arrlen=sizeof(md);
while(1){
//发送
printf("请输入>>>");
bzero(c,sizeof(c));
fgets(c,sizeof(c),stdin);
c[strlen(c)-1]='\0';
rad=sendto(sfd,c,sizeof(c),0,(struct sockaddr*)&md,arrlen);
if(rad<0){
ERR("sendto");
return 0;
}else if(0==rad){
printf("对端关闭\n");
break;
}
}
close(sfd);
return 0;
}