网络编程day5

发布时间:2024年01月19日

select 客户端

#include<myhead.h>

#define PORT 8888
#define IP "192.168.116.203"

#define CLIPORT  6666
#define CLIIP "192.168.116.203"

int main(int argc, const char *argv[])
{
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd==-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;
	}
	printf("connect success\n");



	//定义文件描述符容器
	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);
	int maxfd=sfd;

	char rbuf[128]="";
	char buf[128]="";
	while(1)
	{
		tempfds=readfds;
		int res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(res==-1)
		{
			perror("select error");
			return -1;
		}else if(res == 0)
		{
			printf("timeout\n");
			return -1;
		}
		if(FD_ISSET(0,&readfds))
		{
			bzero(buf,sizeof(buf));
			printf("请输入>>");
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1]='\0';
			send(sfd,buf,sizeof(buf),0);
			if(strcmp(buf,"quit")==0)
			{
				printf("客户端退出\n");
				break;
			}
			printf("发送完成\n");
		}
		if(FD_ISSET(sfd,&readfds))
		{
			bzero(rbuf,sizeof(rbuf));
			recvfrom(sfd,rbuf,sizeof(rbuf),0,0,0);
			printf("收到消息为:%s\n",rbuf);
			if(strcmp(rbuf,"quit")==0)
			{
				break;
			}

		}

	}
END:
	close(sfd);


	return 0;
}

poll服务器

#include<myhead.h>
int main(int argc, const char *argv[])
{
#include<myhead.h>

#define PORT 8888
#define IP "192.168.116.203"
int main(int argc, const char *argv[])
{
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd==-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;
	}
	printf("bind success\n");


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


	struct sockaddr_in cin;
	socklen_t socklen = sizeof(cin);

	struct pollfd pfd[1024];
	pfd[0].fd = 0;
	pfd[0].events = POLLIN;

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

	int newfd = -1;
	while(1)
	{
		int res = poll(pfd,2,-1);
		if(res==-1)
		{
			perror("poll error");
			return -1;
		}
		else if(res==0)
		{
			printf("timeout \n");
			return -1;
		}
		if(pfd[0].revent==POLLIN)
		{
			char buf[128]="";
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1]='\0';
			for(int i=2;i<n;i++)
			{
				send(pfd[i].fd,buf,sizeof(buf),0);
			}
			printf("发送成功\n");
			if(strcmp(buf,"quit")==0)
			{
				close(sfd);
				return -1;
			}
		}
		if(pfd[1].revents==POLLIN)
		{
			int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);
			if(newfd==-1)
			{
				perror("newfd error");
				return -1;
			}
			printf("连接成功\n");
			sin_arr[n]=sin;
			pfd[n].fd=newfd;
			pfd[n].events=POLLIN;
			n++;
		}
		for(int cli=2;cli<n;cli++)
		{
			if(pfd[cli].revents==POLLIN)
			{
				char sbuf[128]-"";
				bzero(sbuf,sizeof(sbuf));
				int red=recv(pfd[cli].fd,sbuf,sizeof(sbuf),0);
				if(red==0)
				{printf("客户端下线\n");
				close(pfd[cli].fd);
				for(int k=cli;k<n;k++)
				{
					pfd[j]=pfd[j+1];
				}
				n--;
				continue;
				}
				printf("[%s:%d]:%s",inetntoa(sin_arr[cli].sin_addr),ntohs(sin_arr[cli].sin_port),sbuf);

				strcat(sbuf,"###");
				send(pfd[cli].fd,sbuf,sizeof(sbuf),0);
				printf("发送完成\n");
			}
		}
	}


	return 0;
}

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