IO多路复用练习

一、通过select搭建TCP服务器

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8808
#define IP "192.168.122.92"

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		perror("socket");
		fprintf(stderr,"socket failed __%d__\n",__LINE__);
		return -1;
	}
	printf("socket success...\n");

	//设置允许端口号复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)  
	{
		perror("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);

	//绑定服务器IP和端口号
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		perror("bind");
		fprintf(stderr,"bind failed __%d__\n",__LINE__);
		return -1;
	}
	printf("bind success...\n");

	//将套接字设置为被动监听状态
	if(listen(sfd,10) < 0)
	{
		perror("listen");
		return -1;
	}
	printf("listen success...\n");

	//从已完成连接的队列中获取一个客户信息,生成一个新的文件描述符,该文件描述符才是与客户端通信的文件描述符
	struct sockaddr_in cin[1021];
	socklen_t addrlen = sizeof(cin);
	int s_res = -1;
	fd_set readfds;
	fd_set tempfds;
	FD_ZERO(&readfds);
	FD_ZERO(&tempfds);
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);
	ssize_t res = 0;
	char buf[128] = "";
	int newfd = -1;
	int maxfd = sfd;
	int j = 0;

	while(1)
	{
		tempfds = readfds;
		s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(s_res < 0)
		{
			perror("select");
			return -1;
		}
		else if(0 == s_res)
		{
			break;
		}
		for(int i=0;i<=maxfd;i++)
		{
			if(!FD_ISSET(i,&tempfds))
				continue;
			if(0 == i)
			{
				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1] = 0;
				printf("%s\n",buf);
			}
			else if(sfd == i)
			{
				newfd = accept(sfd,(struct sockaddr *)&(cin[j]),&addrlen);
				cin[newfd] = cin[j];
				if(newfd < 0)
				{
					perror("accept");
					return -1;
				}
				printf("[%s : %d] accept success\n",inet_ntoa(cin[newfd].sin_addr),ntohs(cin[newfd].sin_port));
				FD_SET(newfd,&readfds);
				maxfd = maxfd > newfd ? maxfd : newfd;
			}
			else
			{
				bzero(buf,sizeof(buf));
				res = recv(i,buf,sizeof(buf),0);
				if(res < 0)
				{
					perror("recv");
					return -1;
				}
				else if(0 == res)
				{
					printf("[%s : %d] client offline\n",inet_ntoa(cin[i].sin_addr),ntohs(cin[i].sin_port));
					close(i);
					FD_CLR(i,&readfds);
					while(FD_ISSET(maxfd,&readfds) == 0 && maxfd-->=0);
					continue;
				}
				printf("[%s : %d] %s\n",inet_ntoa(cin[i].sin_addr),ntohs(cin[i].sin_port),buf);
				//发送数据
				if(send(i,buf,sizeof(buf),0) < 0)
				{
					perror("send");
					return -1;
				}
			}
		}
	}

	//关闭所有文件描述符
	close(sfd);
	return 0;
}

二、通过select搭建TCP客户端

#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8808
#define IP "192.168.122.92"

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd < 0)
	{
		perror("socket");
		fprintf(stderr,"socket failed __%d__\n",__LINE__);
		return -1;
	}
	printf("socket success...\n");

	//设置允许端口号复用
	int reuse = 1;
	if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)  
	{
		perror("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);

	//连接服务器
	if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		perror("connetc");
		return -1;
	}
	printf("connect success...\n");

	ssize_t res = 0;
	char buf[128] = "";
	int s_res = -1;
	fd_set readfds;
	fd_set tempfds;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(cfd,&readfds);

	while(1)
	{
		tempfds = readfds;
		s_res = select(cfd+1,&tempfds,NULL,NULL,NULL);
		if(s_res < 0)
		{
			perror("select");
			return -1;
		}
		else if(0 == s_res)
		{
			break;
		}
		if(FD_ISSET(0,&tempfds))
		{
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = 0;
			//发送数据
			if(send(cfd,buf,sizeof(buf),0) < 0)
			{
				perror("send");
				return -1;
			}
		}
		if(FD_ISSET(cfd,&tempfds))
		{

			//接受数据
			bzero(buf,sizeof(buf));
			res = recv(cfd,buf,sizeof(buf),0);
			if(res < 0)
			{
				perror("recv");
				return -1;
			}
			else if(0 == res)
			{
				printf("[%s : %d] server offline\n",IP,PORT);
				break;
			}
			printf("[%s : %d] %s\n",IP,PORT,buf);
		}
	}

	//关闭所有文件描述符
	close(cfd);
	return 0;
}

三、通过poll搭建客户端

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT 8808
#define IP "192.168.122.92"

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd < 0)
	{
		perror("socket");
		fprintf(stderr,"socket failed __%d__\n",__LINE__);
		return -1;
	}
	printf("socket success...\n");

	//设置允许端口号复用
	int reuse = 1;
	if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)  
	{
		perror("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);

	//连接服务器
	if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		perror("connetc");
		return -1;
	}
	printf("connect success...\n");

	ssize_t res = 0;
	char buf[128] = "";
	int p_res = -1;
	struct pollfd fds[2];
	fds[0].fd = 0;
	fds[0].events = POLLIN;
	fds[1].fd = cfd;
	fds[1].events = POLLIN;


	while(1)
	{

		p_res = poll(fds,2,-1);
		if(p_res < 0)
		{
			perror("select");
			return -1;
		}
		else if(0 == p_res)
		{
			break;
		}
		if(fds[0].revents & POLLIN != 0)
		{
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1] = 0;
			//发送数据
			if(send(cfd,buf,sizeof(buf),0) < 0)
			{
				perror("send");
				return -1;
			}
		}
		if(fds[1].revents & POLLIN)
		{

			//接受数据
			bzero(buf,sizeof(buf));
			res = recv(cfd,buf,sizeof(buf),0);
			if(res < 0)
			{
				perror("recv");
				return -1;
			}
			else if(0 == res)
			{
				printf("[%s : %d] server offline\n",IP,PORT);
				break;
			}
			printf("[%s : %d] %s\n",IP,PORT,buf);
		}
	}

	//关闭所有文件描述符
	close(cfd);
	return 0;
}

你可能感兴趣的:(IO进程线程练习,github)