TCP的poll多路复用实现群聊

 服务器

#include 
#define SERIP "192.168.125.99"
#define SERPORT 9999
int main(int argc, const char *argv[])
{
	int sfd=-1;
	if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}
	printf("sfd=%d\n",sfd);

	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(SERPORT);
	sin.sin_addr.s_addr=inet_addr(SERIP);
	struct sockaddr_in cin;
	socklen_t socklen=sizeof(cin);
	struct sockaddr_in newsin[1024];
	char name[1024][128];

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");

	if(listen(sfd,128)==-1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success\n");
	int res=-1;
	char rbuf[128]="";
	char wbuf[128]="";
	struct pollfd fds[1024];
	for(int i=0;i<1024;i++)
	{
		fds[i].fd=-1;
		fds[i].events=POLLIN;
	}
	fds[0].fd=0;
	fds[0].events=POLLIN;
	//insert_rear("服务器输入",head);

	fds[3].fd=sfd;
	fds[3].events=POLLIN;
	//insert_rear("连接用",head);
	while(1)
	{
		res=poll(fds,1024,-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(rbuf,sizeof(rbuf));
				bzero(wbuf,sizeof(wbuf));
				fgets(rbuf,sizeof(rbuf),stdin);
				rbuf[strlen(rbuf)-1]=0;

				strcat(wbuf,"服务器: ");
				strcat(wbuf,rbuf);
				strcpy(rbuf,wbuf);
				for(int i=4;i<1024;i++)
				{
					send(fds[i].fd,rbuf,sizeof(rbuf),0);
				}
			}
			//负责连接
			else if(fds[3].revents==POLLIN)
			{
				int newfd=-1;
				if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen))==-1)
				{
					perror("connect error");
					return -1;
				}
				newsin[newfd]=cin;
				printf("[%s:%d]connect success\n",inet_ntoa(newsin[newfd].sin_addr),ntohs(newsin[newfd].sin_port));
				fds[newfd].fd=newfd;
			}
		for(int i=4;i<1024;i++)
		{
			//服务器端输入
			if(fds[i].revents!=POLLIN)
			{
				continue;
			}
			{		//表示其余待触发poll	
				bzero(rbuf,sizeof(rbuf));
				char lbuf[128]="";
				int f=recv(fds[i].fd,rbuf,sizeof(rbuf),0);
				if(rbuf[0]=='N')
				{
					char *p=rbuf;
					p=p+1;
					strcpy(name[i],p);
					sprintf(lbuf,"------------%s已上线-----------",name[i]);
					for(int j=4;j<1024;j++)
					{
						send(fds[j].fd,lbuf,sizeof(lbuf),0);
					}
					continue;
				}
				if(strcmp(rbuf,"quit")==0)
				{
					sprintf(lbuf,"[%s:%d]%s已下线",inet_ntoa(newsin[i].sin_addr),ntohs(newsin[i].sin_port),name[i]);
					close(fds[i].fd);
					newsin[i].sin_addr.s_addr=0;
					newsin[i].sin_port=0;
					fds[i].fd=-1;
					for(int k=4;k<1024;k++)
					{		
						send(fds[k].fd,lbuf,sizeof(lbuf),0);	
					}
					send(fds[0].fd,lbuf,sizeof(lbuf),0);
					continue;
				}
				bzero(lbuf,sizeof(lbuf));
				char *p=rbuf;
				p=p+1;
				strcpy(lbuf,p);
				sprintf(wbuf,"[%s:%d]%s:%s",inet_ntoa(newsin[i].sin_addr),ntohs(newsin[i].sin_port),name[i],lbuf);
				for(int j=4;j<1024;j++)
				{
					if(j==i)
					{
						continue;
					}
					send(fds[j].fd,wbuf,sizeof(wbuf),0);
				}
			}
		}
	}
	close(sfd);
	return 0;
}

客户端

#include 
#define SERIP "192.168.125.99"
#define SERPORT 9999

int main(int argc, const char *argv[])
{
	int cfd=-1;
	if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		perror("socket error");
		return -1;
	}
	printf("cfd=%d\n",cfd);
	int reuse=1;
	if(setsockopt(cfd,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(SERPORT);
	sin.sin_addr.s_addr=inet_addr(SERIP);
	if((connect(cfd,(struct sockaddr*)&sin,sizeof(sin)))==-1)
	{
		perror("connect error");
		return -1;
	}
	printf("connect success\n");
	struct pollfd fds[2];
	fds[0].fd=0;
	fds[0].events=POLLIN;
	fds[1].fd=cfd;
	fds[1].events=POLLIN;
	int flag=0;
	char rbuf[128]="";
	char buf[128]="";
	char n[129]="N";
	char s[129]="S";
	char q[129]="quit";
	int res=-1;
	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)
		{
			if(flag==0)
			{//输入名字
				bzero(buf,sizeof(buf));
				bzero(rbuf,sizeof(rbuf));

				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1]=0;
				strcat(rbuf,n);
				strcat(rbuf,buf);
				send(cfd,rbuf,sizeof(rbuf),0);
				flag=1;
			}
			else
			{//输入信息
				bzero(buf,sizeof(buf));
				bzero(rbuf,sizeof(rbuf));
				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1]=0;
				if(strcmp(q,buf)==0)
				{
					send(cfd,buf,sizeof(buf),0);
					goto END;
				}
				strcat(rbuf,s);
				strcat(rbuf,buf);
				send(cfd,rbuf,sizeof(rbuf),0);
			}
		}
		if(fds[1].revents==POLLIN)
		{

			bzero(rbuf,sizeof(rbuf));
			recv(cfd,rbuf,sizeof(rbuf),0);
			printf("%s\n",rbuf);
		}
	}
END:
	close(cfd);
	return 0;
}

你可能感兴趣的:(c语言)