网络编程12.26

select的TCP客户端

#include "head.h"

#define IP "192.168.124.76" 

#define PORT 6666
int main(int argc, const char *argv[])
{
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		_errmsg("socket");
		return -1;
	}


	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	socklen_t addrlen=sizeof(sin);

	if(connect(sfd,(struct sockaddr*)&sin,addrlen)<0)
	{
		_errmsg("connect");
		return -1;
	}

	fd_set readfds;
		char buf[128];
		ssize_t res;

	while(1)
	{
		FD_ZERO(&readfds);
		FD_SET(sfd,&readfds);
		FD_SET(0,&readfds);

		select(sfd+1,&readfds,NULL,NULL,NULL);

		if(FD_ISSET(0,&readfds))
		{
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			if(send(sfd,buf,sizeof(buf),0)<0)
			{
				_errmsg("send");
				return -1;
			}
		}

		if(FD_ISSET(sfd,&readfds))
		{
			bzero(buf,sizeof(buf));
			if(recv(sfd,buf,sizeof(buf),0)<0)
			{
				_errmsg("recv");
				return -1;
			}
			printf("%s\n",buf);
		}

		
	}

	close(sfd);

	return 0;
}

select的TCP服务器

#include "head.h"

#define IP "192.168.124.76" 

#define PORT 6666

void hand(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);

}




int main(int argc, const char *argv[])
{
	if(signal(17,hand)<0)
	{
		_errmsg("signal");
		return -1;
	}
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		_errmsg("socket");
		return -1;
	}

	//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		_errmsg("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);
	socklen_t addrlen1=sizeof(sin);
	if(bind(sfd,(struct sockaddr*)&sin,addrlen1)<0)
	{
		_errmsg("bind");
		return -1;
	}

	if(listen(sfd,128)<0)
	{
		_errmsg("listen");
		return -1;
	}
	//创建一个读集合
	//readfds必须清空
	fd_set readfds,tempfds;
	FD_ZERO(&readfds);
	//将需要监测的文件描述符填充到读集合中
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);


	struct sockaddr_in nin[1024-3];
	socklen_t addrlen2=sizeof(nin[0]);

	char buf[128];
	ssize_t res;
	bzero(buf,sizeof(buf));
	pid_t cpid;
	int newfd=-1;
	int s_res=0;
	int maxfd = sfd;
	struct sockaddr_in cin;
	while(1)
	{
		tempfds = readfds;
		s_res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
		if(s_res<0)
		{
			_errmsg("select");
			return -1;
		}
		else if(s_res == 0)
		{
			printf("time out...\n");
			return -1;
		}

		for(int i=0;i<=maxfd;i++)
		{
			if(FD_ISSET(i,&tempfds) == 0)
			{
				continue;
			}
			if(i==0)
			{
				printf("键盘输入\n");
				fgets(buf,sizeof(buf),stdin);
				buf[strlen(buf)-1]=0;
				printf("%s\n",buf);

			}
			else if(i==sfd)
			{
				printf("%d\n",i);
				printf("客户端链接\n");
				newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen2);
				if(newfd<0)
				{
					_errmsg("accept");
					return -1;
				}
				printf("[%s:%d] %d \n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
				nin[newfd-4] = cin;

				FD_SET(newfd,&readfds);
				maxfd = maxfd>newfd? maxfd:newfd ;
			}
			else
			{
				printf("%d\n",i);
				printf("客户端读写操作\n");
				bzero(buf,sizeof(buf));
				if((res=recv(i,buf,sizeof(buf),0))<0)
				{
					_errmsg("recv");
					return -1;
				}
				else if(res==0)
				{
					printf("[%s:%d] %d \n",inet_ntoa(nin[i-4].sin_addr),ntohs(nin[i-4].sin_port),i);
					printf("客户端下线\n");
					close(i);
					FD_CLR(i,&readfds);

					while(FD_ISSET(maxfd,&readfds)==0 && (maxfd)-->0);
					break;
				}

				printf("[%s:%d] %d : %s \n",inet_ntoa(nin[i-4].sin_addr),ntohs(nin[i-4].sin_port),i,buf);
				strcat(buf,"hahaha");
				if(send(i,buf,sizeof(buf),0)<0)
				{
					_errmsg("send");
					return -1;
				}
			}
		}
	}



	close(sfd);

	return 0;
}

你可能感兴趣的:(网络)