12.20 day5 IO网络编程

select服务端

#include 
#define PORT 9999
#define IP "192.168.112.197"
int main(int argc, const char *argv[])
{
	int sfd=-1;
	if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}

	int reuse =1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
	{
		perror("setsockopt error");
		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(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("bind error");
		return -1;
	}

	if(listen(sfd,128)==-1)
	{
		perror("listen error");
		return -1;
	}

	struct sockaddr_in cin;
	socklen_t socklen=sizeof(cin);
	int newfd=-1;
	char a[128]="";
	fd_set readfds;
	FD_ZERO(&readfds);
	FD_SET(sfd,&readfds);
	FD_SET(0,&readfds);
	int res=0;
	fd_set tempfds;
	int maxfd=sfd;
	char ra[128]="";
	struct sockaddr_in arr_cin[1024];
	while(1)
	{
		tempfds=readfds;
		res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(res==-1)
		{
			perror("select error");
			return -1;
		}else if(res==0)
		{
			printf("time out\n");
			return -1;
		}
		for(int index=0;index<=maxfd;index++)
		{
			if(!FD_ISSET(index,&tempfds))
			{
				continue;
			}
			if(index==sfd)
			{
				if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen))==-1)
				{
					perror("accept error");
					return -1;
				}
				arr_cin[newfd]=cin;
				printf("[%s:%d]连接成功newfd=%d\n",inet_ntoa(arr_cin[newfd].sin_addr),ntohs(arr_cin[newfd].sin_port),newfd);

				FD_SET(newfd,&readfds);
				if(newfd>maxfd)
				{
					maxfd=newfd;
				}
			}else if(index==0)
			{
				scanf("%s",a);
				printf("触发终端输入:%s\n",a);

				for(int j=4;j<=maxfd&&FD_ISSET(j,&readfds);j++)
				{
					send(j,a,sizeof(a),0);
				}
			}else
			{
				bzero(ra,sizeof(ra));
				int res=recv(index,ra,sizeof(ra),0);
				if(res==0)
				{
					printf("客户端下线\n");
					close(index);
					FD_CLR(index,&readfds);
					for(int i=maxfd;i>0;i--)
					{
						if(FD_ISSET(i,&readfds))
						{
						maxfd=i;
						break;
						}
					}
					continue;
				}
				printf("[%s:%d]:%s\n",inet_ntoa(arr_cin[index].sin_addr),ntohs(arr_cin[index].sin_port),ra);
				strcat(ra,"*_*");
				send(index,ra,sizeof(ra),0);
			}
		}
	}
	return 0;
}

 select客户端

#include 
#define KPORT 6666
#define KIP "192.168.112.197"

#define FPORT 9999
#define FIP "192.168.112.197"
int main(int argc, const char *argv[])
{
	int cfd=-1;
	if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}

	int reuse=1;
	if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
	{
		perror("setsockopt error");
		return -1;
	}

	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(KPORT);
	cin.sin_addr.s_addr=inet_addr(KIP);
	if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
	{
		perror("bind error ");
		return -1;
	}


	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(FPORT);
	sin.sin_addr.s_addr=inet_addr(FIP);
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}
	char a[128]="";
	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	FD_SET(cfd,&readfds);
	FD_SET(0,&readfds);

	int res=0;
	struct sockaddr_in arr_cin[1024];

	while(1)
	{
		tempfds=readfds;
		res=select(cfd+1,&tempfds,NULL,NULL,NULL);
		if(res==-1)
		{
			perror("select error");
			return -1;
		}else if(res==0)
		{
			printf("time out\n");
			return -1;
		}
		if(FD_ISSET(0,&tempfds))
		{
			bzero(a,sizeof(a));
			fgets(a,sizeof(a),stdin);
			a[strlen(a)-1]=0;
			send(cfd,a,sizeof(a),0);
		}
		if(FD_ISSET(cfd,&tempfds))
		{
			bzero(a,sizeof(a));
			res=recv(cfd,a,sizeof(a),0);
			if(res==0)
			{
				printf("服务器已下线\n");
				break;
			}
			printf("[%s:%d]:%s\n",FIP,FPORT,a);
		}
	}
	close(cfd);
	return 0;
}

12.20 day5 IO网络编程_第1张图片poll服务端

#include 
#define PORT  9999
#define IP "192.168.112.197"
void del_arr(struct pollfd fds[],int *max,int index)
{
	for(int i=index;i<(*max)-1;i++)
	{
		fds[i]=fds[i+1];
	}
	(*max)-=1;
}
int main(int argc, const char *argv[])
{
	int sfd=-1;
	if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}

	int reuse=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
	{
		perror("setsockopt error");
		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(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("bind error");
		return -1;
	}

	if(listen(sfd,128)==-1)
	{
		perror("listen error");
		return -1;
	}

	struct sockaddr_in cin;
	socklen_t socklen=sizeof(cin);
	char wa[128]="";

	struct pollfd fds[1024];

	fds[0].fd=0;
	fds[0].events=POLLIN;

	fds[1].fd=sfd;
	fds[1].events=POLLIN;

	int res=-1;

	int newfd=-1;
	int max=2;
	struct sockaddr_in arr_cin[1024];

	while(1)
	{
		res=poll(fds,max,-1);
		if(res<0)
		{
			perror("poll error");
			return -1;
		}else if(res==0)
		{
			printf("time out\n");
			return -1;
		}
		for(int index=0;index

poll客户端

#include 
#define FPORT  9999
#define FIP "192.168.112.197"

#define CPORT 8888
#define CIP "192.168.112.197"
int main(int argc, const char *argv[])
{
	int cfd=-1;
	if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}

	int reuse=1;
	if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
	{
		perror("setsockopt error");
		return -1;
	}

	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(CPORT);
	cin.sin_addr.s_addr=inet_addr(CIP);
	if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
	{
		perror("bind error");
		return -1;
	}

	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(FPORT);
	sin.sin_addr.s_addr=inet_addr(FIP);
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}


	struct pollfd fds[2];

	fds[0].fd=0;
	fds[0].events=POLLIN;

	fds[1].fd=cfd;
	fds[1].events=POLLIN;

	int res=-1;

	char wa[128]="";

	while(1)
	{
		res=poll(fds,2,-1);
		if(res<0)
		{
			perror("poll error");
			return -1;
		}else if(res==0)
		{
			printf("time out\n");
			return -1;
		}
		if(fds[0].revents==POLLIN)
		{
			bzero(wa,sizeof(wa));

			fgets(wa,sizeof(wa),stdin);
			wa[strlen(wa)-1]=0;
			send(cfd,wa,sizeof(wa),0);
		}
		if(fds[1].revents==POLLIN)
		{
			bzero(wa,sizeof(wa));
			int res=recv(cfd,wa,sizeof(wa),0);
			if(res==0)
			{
				printf("服务器已下线\n");
				break;
			}
			printf("[%s:%d]:%s\n",FIP,FPORT,wa);
		}
	}
	close(cfd);

	return 0;
}

你可能感兴趣的:(网络)