select和poll

2023-12-21 01:01:23

slelect实现客户端

#include <myhead.h>
#define PORT 8888
#define PORT1 9999
#define IP   "192.168.125.147"
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(PORT1);
	cin.sin_addr.s_addr=inet_addr(IP);
	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(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}
	char buf[128]="";
	char rbuf[128]="";
	fd_set readfds,temp;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(cfd,&readfds);
	while(1)
	{
		temp=readfds;
		if(select(cfd+1,&temp,NULL,NULL,NULL)==-1)
		{
			perror("select error");
			return -1;
		}
		if(FD_ISSET(0,&temp))
		{
			bzero(buf,128);
			scanf("%s",buf);
			if(strcmp(buf,"quit")==0)
			{
				puts("退出");
				break;
			}
			send(cfd,buf,sizeof(buf),0);
		}
		if(FD_ISSET(cfd,&temp))
		{
			bzero(rbuf,128);
			recv(cfd,rbuf,sizeof(rbuf),0);
			printf("服务器:%s\n",rbuf);
		}
	}
	close(cfd);
	return 0;
}

poll实现服务器

ubuntu@ubuntu:day5$ cat 5.poll.c 
#include <myhead.h>
#define PORT 8888
#define IP "192.168.125.147"
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 len=sizeof(cin);
	struct sockaddr_in sock[1024];
	char ok[50];
	int newfd=-1;
	int maxfd=4;
	struct pollfd fds[1024];
	fds[0].fd=0;
	fds[0].events=POLLIN;
	fds[1].fd=sfd;
	fds[1].events=POLLIN;
	int count=2;
	puts("准备就绪");
	while(1)
	{
		if(poll(fds,count,-1)==-1)
		{
			perror("poll error");
			return -1;
		}
		for(int i=0;i<count;i++)
		{	
			if(fds[i].revents!=POLLIN)
			{
				continue;
			}
			if(i==0)
			{
				scanf("%s",ok);
		//		printf("终端输入%s\n",ok);
				if(strcmp(ok,"quit")==0)
					goto aa;
				for(int j=2;j<count;j++)
				{
					send(fds[j].fd,ok,sizeof(ok),0);
				}
			}
			else if(i==1)
			{
				if((newfd=accept(sfd,(struct sockaddr*)&cin,&len))==-1)
				{
					perror("accept error");
					return -1;
				}
				printf("[%s-%d]:连接成功,newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
				fds[count].fd=newfd;
				fds[count].events=POLLIN;
				count++;
				sock[newfd]=cin;
			}
			else
			{
				char buf[128]="";
				int res=recv(fds[i].fd,buf,sizeof(buf),0);
				if(res==0)
				{
					close(fds[i]);
					printf("[%s-%d]:退出\n",inet_ntoa(sock[fds[i].fd].sin_addr),ntohs(sock[fds[i].fd].sin_port));
					fds[i]=fds[count--];
					continue;
				}
				printf("[%s-%d]:%s\n",inet_ntoa(sock[fds[i].fd].sin_addr),ntohs(sock[fds[i].fd].sin_port),buf);
				strcat(buf,"sb");
				send(fds[i].fd,buf,sizeof(buf),0);
			}
		}
	}
aa:
	close(sfd);
	return 0;
}

文章来源:https://blog.csdn.net/m0_64059184/article/details/135119020
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。