网络编程day5作业

1. 根据select TCP服务器流程图编写服务器(上交)

#include 
#define ERR_MSG(msg) do{\
	fprintf(stderr,"__%d__",__LINE__);\
	perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.114.50"
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket create success! sfd=%d\n",sfd);
	//允许端口快速被复用
	int reuse=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速被复用成功\n");
	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))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	//创建一个读的集合
	fd_set readfds,tempfds;
	//清空集合
	FD_ZERO(&readfds);
	//将需要监测的文件描述符放入到集合中
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);
	int maxfd=sfd;
	char buf[128]="";
	ssize_t res=-1;
	int newfd=-1;
	int s_res=-1;
	struct sockaddr_in savaCin[1024];
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);
	while(1)
	{
		tempfds=readfds;
		s_res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(s_res<0)
		{
			ERR_MSG("select");
			return -1;
		}
		if(0==s_res)
		{
			printf("time out..\n");
			break;
		}
		printf("__%d__\n",__LINE__);
		for(int i=0;i<=maxfd;i++)
		{
			if(FD_ISSET(i,&tempfds)==0)
			{
				continue;
			}
			if(0==i)
			{
				printf("触发键盘输入事件\n");
				bzero(buf,sizeof(buf));
				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1]=0;
				printf("输出:%s\n",buf);
			}
			else if(sfd==i)
			{
				printf("触发客户端连接事件\n");
				newfd=accept(i,(struct sockaddr*)&cin,&addrlen);
				if(newfd<0)
				{
					ERR_MSG("accept");
					return -1;
				}
				printf("[%s:%d]客户端连接成功newfd=%d\n",\
						inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
				FD_SET(newfd,&readfds);
				savaCin[newfd]=cin;
				maxfd=maxfd>newfd?maxfd:newfd;
			}
			else
			{
				printf("触发客户端交互事件\n");
				//清空字符串
				bzero(buf,sizeof(buf));
				//接收
				res=recv(i,buf,sizeof(buf),0);
				if(res<0)
				{
					ERR_MSG("recv");
					return -1;
				}
				if(0==res)
				{
					printf("[%s:%d]客户端下线newfd=%d\n",\
							inet_ntoa(savaCin[i].sin_addr),ntohs(savaCin[i].sin_port),i);
					close(i);
					FD_CLR(i,&readfds);
					for(;maxfd>=0;maxfd--)
					{
						if(FD_ISSET(maxfd,&readfds))
						{
							break;
						}
					}
					continue;
				}
				printf("[%s:%d]newfd=%d:%s\n",\
						inet_ntoa(savaCin[i].sin_addr),ntohs(savaCin[i].sin_port),i,buf);
				//发送
				strcat(buf,"zyz");
				if(send(i,buf,sizeof(buf),0)<0)
				{
					ERR_MSG("send");
					return -1;
				}
				printf("send success\n");
			}
		}
	}
	if(close(sfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	return 0;
}

思维导图:https://mubu.com/app/edit/home/4LfFX79g2CD#m

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