1.18 day5 IO网络编程

select客户端

#include 
#define PORT 9999
#define IP "192.168.122.46"
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(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}

	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	FD_SET(sfd,&readfds);
	FD_SET(0,&readfds);
	
	char buf[128]="";
	while(1)
	{
		tempfds=readfds;
		int res=select(sfd+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(sfd,&tempfds))
		{
			bzero(buf,sizeof(buf));
			recv(sfd,buf,sizeof(buf),0);
			printf("[%s:%d]:%s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
		}
		if(FD_ISSET(0,&tempfds))
		{
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1]=0;
			send(sfd,buf,sizeof(buf),0);
		}
	}

	return 0;
}

poll服务端

#include 
#define PORT 9999
#define IP "192.168.122.46"
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);
	struct sockaddr_in cin_add[1024];
	int newfd=-1;
	int maxfd=sfd;
	
	struct pollfd fds[1024];
	for(int i=0;i<1024;i++){
		fds[i].fd=-1;
	
	}
	fds[0].fd=0;
	fds[0].events=POLLIN;
	fds[1].fd=sfd;
	fds[1].events=POLLIN;
int	count=2;
	while(1)
	{
		int res=poll(fds,1024,-1);
		if(res==-1)
		{
			perror("poll error");
			return -1;
		}else if(res==0)
		{
			printf("time out\n");
			return -1;
		}

		if(fds[1].revents==POLLIN)
		{
			newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);	
			if(newfd==-1)
			{
				perror("accept error");
				return -1;
			}
			printf("[%s:%d]:请求连接newfd:%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);

			for(count=2;count<1024;count++)
			{
				if(fds[count].fd==-1)
				{
					fds[count].fd=newfd;
					fds[count].events=POLLIN;
					break;
				}
			}
			
		}
		if(fds[0].revents==POLLIN)
		{
			char wbuf[128]="";
			bzero(wbuf,sizeof(wbuf));
			fgets(wbuf,sizeof(wbuf),stdin);
			wbuf[strlen(wbuf)-1]=0;
			for(int i=4;i<=1024;i++)
			{
				sendto(i,wbuf,sizeof(wbuf),0,(struct sockaddr*)&cin_add[i],sizeof(cin_add[i]));
			}
		}
		for(int cli=2;cli<=1024;cli++)
		{
			if(fds[cli].revents==POLLIN&&fds[cli].fd!=-1)
			{
				char buf[128]="";

				bzero(buf,sizeof(buf));
				int res=recv(fds[cli].fd,buf,sizeof(buf),0);
				if(res==0)
				{
					printf("客户端下线\n");
					close(fds[cli].fd);
					fds[cli].fd=-1;
				}
				printf("[%s:%d]:%s\n",inet_ntoa(cin_add[cli].sin_addr),ntohs(cin_add[cli].sin_port),buf);
			}
		}

	}


	return 0;
}

你可能感兴趣的:(网络,php,开发语言)