C++DAY37

广播发送

#include "head.h"
#define PORT 2580
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
	//创建套接字
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);

	//设置允许广播
	int broad = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("setsockopt success\n");

	//填充接收方的地址信息,给sendto函数使用
	//真实的地址信息结构体根据地址族指定AF_INET: man 7 IP
	struct sockaddr_in sin;
	sin.sin_family = AF_INET; //必须填AF_INET
	sin.sin_port = htons(PORT); //接收方的端口
	sin.sin_addr.s_addr = inet_addr(IP); //接收方的IP

	char buf[128]="";
	ssize_t res = 0;
	while(1)
	{
		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,sizeof(sin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");
	}

	close(sfd);

	return 0;
}

广播接收

#include "head.h"
#define PORT 2580
#define IP "192.168.124.255"
int main(int argc, const char *argv[])
{
	//创建套接字
	int sfd = socket(AF_INET,SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);


	//填充接收方的地址信息,给sendto函数使用
	//真实的地址信息结构体根据地址族指定AF_INET: man 7 IP
	struct sockaddr_in sin;
	sin.sin_family = AF_INET; //必须填AF_INET
	sin.sin_port = htons(PORT); //接收方的端口
	sin.sin_addr.s_addr = inet_addr(IP); //接收方的IP

	//绑定接收方自身的地址信息
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	struct sockaddr_in stn;
	socklen_t addrlen = sizeof(stn);

	char buf[128]="";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addrlen);
		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("[%s:%d]: %s\n",inet_ntoa(stn.sin_addr),ntohs(stn.sin_port),buf);
	}

	close(sfd);

	return 0;
}

组播发送

#include "head.h"
#define SER_PORT 2580
#define SER_IP "224.1.2.3"//组播IP
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd = socket(AF_INET, SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SER_PORT);
	sin.sin_addr.s_addr = inet_addr(SER_IP);//组播IP

	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		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,sizeof(sin)) < 0)
		{
			ERR_MSG("sendto");
			return -1;
		}
		printf("sendto success\n");

	}
	close(sfd);//关闭套接字
	return 0;
}

 组播接收

#include "head.h"
#define PORT 2580
#define GRP_IP "224.1.2.3"//组播IP
#define LOL_IP "192.168.124.34" //本机IP
int main(int argc, const char *argv[])
{
	//创建报式套接字
	int sfd = socket(AF_INET, SOCK_DGRAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);

	//加入多播组
	struct ip_mreqn mq;

	mq.imr_multiaddr.s_addr = inet_addr(GRP_IP); //组播IP
	mq.imr_address.s_addr = inet_addr(LOL_IP); //本机IP,ifconfig
	mq.imr_ifindex = 2;//网络设备索引号
	if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("加入多播组 [%s] 成功\n",GRP_IP);

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(GRP_IP);//组播IP

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success");

	struct sockaddr_in stn;
	socklen_t stnlen = sizeof(stn);

	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&stn,&stnlen);
		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("[%s:%d] : %s\n",inet_ntoa(stn.sin_addr),ntohs(stn.sin_port),buf);
	}
	close(sfd);//关闭套接字
	return 0;
}

 多进程

#include "head.h"
#define PORT 2580
#define IP "192.168.124.32"	
int deal_cli_msg(int newfd,struct sockaddr_in cin);

void handler(int sig)
{
	//循环回收僵尸进程
	//有子进程,没有僵尸进程 == 0
	//没有子进程,也没有僵尸进程 == -1
	while(waitpid(-1,NULL,WNOHANG) > 0);
	return ;
}


int main(int argc, const char *argv[])
{
	//捕获17号,SIGCHLD信号
	if(signal(SIGCHLD,handler) == SIG_ERR)
	{
		ERR_MSG("signal");
		return -1;
	}


	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);

	//允许端口快速复用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速复用\n");

	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");

	if(listen(sfd,128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int newfd = -1;
	pid_t cpid = -1;
	while(1)
	{
		newfd = accept(sfd,(struct sockaddr*)&cin ,&addrlen);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s:%d] newfd=%d 客户端连接成功__%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
		
		//能运行到当前位置,则代表有客户端连接成功
		//此时需要创建一个子进程,专门用于与客户端交互
		cpid = fork();
		if(0 == cpid)
		{
			close(sfd); //子进程只负责交互,sfd没有用
			deal_cli_msg(newfd,cin);
			close(newfd);
			exit(0); //退出子进程,子进程只负责交互,不允许回到accept函数
		}
		else if(cpid < 0)
		{
			ERR_MSG("fork");
			return -1;
		}
		close(newfd); //在父进程中newfd没有用
	}
	return 0;
}

//子进程负责与客户端交互的函数
int deal_cli_msg(int newfd,struct sockaddr_in cin)
{
	char buf[128] = "";
	ssize_t res =0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接收数据
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("[%s:%d] newfd=%d 客户端下线__%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
			break;
		}
		printf("[%s:%d] newfd=%d : %s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
		if(strcmp(buf,"quit") == 0)
			break;
		//发送数据
		strcat(buf,"*_*");//数据可以选择从终端获取
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");

	}
	return 0;
}

多线程

#include "head.h"
#define PORT 2580
#define IP "192.168.124.32"	

struct Climsg
{
	int newfd;
	struct sockaddr_in cin;
};

void* deal_cli_msg(void* arg);


int main(int argc, const char *argv[])
{

	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success sfd=%d\n",sfd);

	//允许端口快速复用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速复用\n");

	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");

	//将套接字转换成被动监听状态
	if(listen(sfd,128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int newfd = -1;
	pthread_t tid; //存储线程tid号
	struct Climsg info;
	while(1)
	{
		newfd = accept(sfd,(struct sockaddr*)&cin ,&addrlen);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s:%d] newfd=%d 客户端连接成功__%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
		
		info.newfd = newfd;
		info.cin = cin;
		//能运行到当前位置,则代表有客户端连接成功
		//此时需要创建一个子进程,专门用于与客户端交互
		if(pthread_create(&tid,NULL,deal_cli_msg,(void*)&info) != 0)
		{
			fprintf(stderr,"pthread_create failed__%d__\n",__LINE__);
			return -1;
		}
		pthread_detach(tid); //分离线程
	}
	close(sfd);
	return 0;
}

//线程执行体--->分支线程只负责交互
void* deal_cli_msg(void* arg)
{
	int newfd = ((struct Climsg*)arg)->newfd;
	struct sockaddr_in cin = ((struct Climsg*)arg)->cin;

	char buf[128] = "";
	ssize_t res =0;
	
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接收数据
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0 == res)
		{
			printf("[%s:%d] newfd=%d 客户端下线__%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
			break;
		}
		printf("[%s:%d] newfd=%d : %s __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
		if(strcmp(buf,"quit") == 0)
			break;
		//发送数据
		strcat(buf,"*_*");//数据可以选择从终端获取
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			break;
		}
		printf("send success\n");
	}
	close(newfd);
	pthread_exit(NULL);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值