12-20

#include 
#define SERIP "192.168.125.12"
#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);

	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 newfd=-1;
	if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen))==-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=newfd;
	fds[1].events=POLLIN;

	int res=-1;
	char rbuf[128]="";
	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)
		{
			bzero(rbuf,sizeof(rbuf));
			fgets(rbuf,sizeof(rbuf),stdin);
			rbuf[strlen(rbuf)-1]=0;
			send(newfd,rbuf,sizeof(rbuf),0);
		}
		if(fds[1].revents==POLLIN)
		{
			bzero(rbuf,sizeof(rbuf));
			int res=recvfrom(newfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen);
			if(res==0)
			{
				printf("[%s:%d]已下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
				break;
			}
			printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
		}
	}
	close(newfd);
	close(sfd);
	return 0;
}

12-20_第1张图片

#include 
#define PORT 9999
#define IP "192.168.125.12"
#define CLIPORT 6666
#define CLIIP "192.168.125.12"
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 cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(CLIPORT);
	cin.sin_addr.s_addr=inet_addr(CLIIP);
	if(bind(sfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\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 socklen=sizeof(sin);
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
	{
		perror("connect error");
		return -1;
	}
	printf("connect success\n");

	char buf[128]="";

	fd_set readfds;
	FD_ZERO(&readfds);
	FD_SET(0,&readfds);
	FD_SET(sfd,&readfds);
	int res=0;
	fd_set tempfds;
	char rbuf[128]="";
	while(1)
	{
		tempfds=readfds;
		res=select(sfd+1,&tempfds,NULL,NULL,NULL);
		if(res==-1)
		{
			perror("select error");
			return -1;
		}
		else if(res==0)
		{
			printf("time out\n");
			return -1;
		}
		if(FD_ISSET(sfd,&tempfds))
		{
		printf("res=%d\n",res);
			bzero(rbuf,sizeof(rbuf));
			int res=recv(sfd,rbuf,sizeof(rbuf),0);
			if(res==0)
			{
				printf("服务器下线\n");
				break;
			}
			printf("[%s:%d]:%s\n",IP,PORT,rbuf);
		}
		if(FD_ISSET(0,&tempfds))
		{
		printf("res=%d\n",res);
			fgets(buf,sizeof(buf),stdin);
			buf[strlen(buf)-1]=0;
			printf("触发终端输入:%s\n",buf);
			send(sfd,buf,sizeof(buf),0);
		}
	}
	close(sfd);
	return 0;
}

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