网络编程:广播与组播

广播发送(客户端):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define CER_PORT 8888
#define CER_IP "192.168.8.255"

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sockfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd<0){
		ERR_MSG("socket");
		return -1;
	}
	
	//将广播设置为可发送
	int value=1;
	socklen_t len1=sizeof(value);
	if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&value,len1)<0){
		ERR_MSG("setsockopt");
		return -1;
	}
	

	//绑定IP及端口信息
	struct sockaddr_in client;
	client.sin_family=SOCK_DGRAM;
	client.sin_port=htons(CER_PORT);
	client.sin_addr.s_addr=inet_addr(CER_IP);
	char buf[128]="";
	while(1){
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client,sizeof(client))<0){
			ERR_MSG("sendto");
			return -1;
		}
	}
	close(sockfd);
	return 0;
}

广播接收(服务器):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define CER_PORT 8888
#define CER_IP "192.168.8.255"

int main(int argc, const char *argv[])
{
	//创建套接字
	int sockfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd<0){
		ERR_MSG("socket");
		return -1;
	}
	

	//绑定IP及端口信息
	struct sockaddr_in server;
	server.sin_family=SOCK_DGRAM;
	server.sin_port=htons(CER_PORT);
	server.sin_addr.s_addr=inet_addr(CER_IP);
	if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	char buf[128]="";
	struct sockaddr_in client;
	socklen_t len=sizeof(client);
	while(1){
		bzero(buf,sizeof(buf));
		if(recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client,&len)<0){
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("%s\n",buf);
	}
	close(sockfd);
	return 0;
}

组播发送(客户端):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define CER_PORT 8888
#define CER_IP "192.168.8.191"
#define GRO_IP "224.1.2.3"

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sockfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd<0){
		ERR_MSG("socket");
		return -1;
	}
	

	//绑定IP及端口信息
	struct sockaddr_in client;
	client.sin_family=SOCK_DGRAM;
	client.sin_port=htons(CER_PORT);
	client.sin_addr.s_addr=inet_addr(GRO_IP);
	char buf[128]="";
	while(1){
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		if(sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client,sizeof(client))<0){
			ERR_MSG("sendto");
			return -1;
		}
	}
	close(sockfd);
	return 0;
}

组播接收(服务器):

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>

#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define CER_PORT 8888
#define CER_IP "192.168.8.191"
#define GRO_IP "224.1.2.3"

int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sockfd=socket(AF_INET,SOCK_DGRAM,0);
	if(sockfd<0){
		ERR_MSG("socket");
		return -1;
	}
	
	//加入组播
	struct ip_mreqn redio;
	redio.imr_multiaddr.s_addr=inet_addr(GRO_IP);
	redio.imr_address.s_addr=inet_addr(CER_IP);
	redio.imr_ifindex=if_nametoindex("ens33");
	if(setsockopt(sockfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&redio,sizeof(redio))<0){
		ERR_MSG("setsockopt");
		return -1;
	}

	//绑定IP及端口信息
	struct sockaddr_in server;
	server.sin_family=SOCK_DGRAM;
	server.sin_port=htons(CER_PORT);
	server.sin_addr.s_addr=inet_addr(GRO_IP);
	if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//接收客户端信息
	struct sockaddr_in client;
	socklen_t len=sizeof(client);
	char buf[128]="";
	while(1){
		bzero(buf,sizeof(buf));
		if(recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&client,&len)<0){
			ERR_MSG("sendto");
			return -1;
		}
		printf("%s\n",buf);
	}
	close(sockfd);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值