(unix网络编程)tcp回射程序三:多路复用

使用select来多路复用,可以提高IO的效率

服务器端代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>

#define MAXLINE 4096
#define SERV_PORT 5000
#define LISTENQ 5

int main(int argc, char **argv)
{
	int i, maxi, maxfd, listenfd, connfd, sockfd;
	int nready, client[FD_SETSIZE];
	ssize_t n;
	fd_set rset, allset;
	char buf[MAXLINE];
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;

	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("socket error:%s\n", strerror(errno));
		return -1;
	}

	memset(&servaddr, 0x00, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(SERV_PORT);

	if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
		printf("bind error:%s\n", strerror(errno));
		return -1;
	}

	if (listen(listenfd, LISTENQ) < 0) {
		printf("listen error:%s\n", strerror(errno));
		return -1;
	}

	maxfd = listenfd;
	maxi = -1;
	for (i = 0; i < FD_SETSIZE; i++)
		client[i] = -1;
	
	FD_ZERO(&allset);
	FD_SET(listenfd, &allset);

	for(;;) {
		rset = allset;
		if((nready = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) {
			printf("select error: %s\n", strerror(errno));
		}

		if (FD_ISSET(listenfd, &rset)) {
			clilen = sizeof(cliaddr);
			if ((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
				printf("accept error: %s\n", strerror(errno));
			}

			for (i = 0; i < FD_SETSIZE; i++) {
				if (client[i] < 0) {
					client[i] = connfd;
					break;
				}
			}

			if (i == FD_SETSIZE)
				printf("too many clients\n");

			FD_SET(connfd, &allset);

			if (connfd > maxfd)
				maxfd = connfd;

			if (i > maxi)
				maxi = i;

			if (--nready <= 0)
				continue;
		}

		for (i = 0; i <= maxi; i++) {
			if ((sockfd = client[i]) < 0)
				continue;

			if (FD_ISSET(sockfd, &rset)) {
				if ((n = read(sockfd, buf, MAXLINE)) <= 0) {
					printf("client quit\n");
					if (close(sockfd) < 0) {
						printf("close error: %s\n", strerror(errno));
					}
					FD_CLR(sockfd, &allset);
					client[i] = -1;
				} else {
					if (write(sockfd, buf, n) < 0) {
						printf("write error: %s\n", strerror(errno));
					}
				}

				if (--nready <= 0) {
					break;
				}
			}
		}
	}
}

客户端代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/select.h>
#include <unistd.h>

#define SERV_PORT 5000
#define MAXLINE 4096


void str_cli(FILE *fp, int sockfd) {
	const char *quit_code = "QUIT\n";
	char buf[MAXLINE];
	int maxfdp1, stdineof;
	fd_set rset;
	int n;

	stdineof = 0;
	FD_ZERO(&rset);

	for (;;) {
		if (stdineof == 0)
			FD_SET(fileno(fp), &rset);

		FD_SET(sockfd, &rset);
		maxfdp1 = fileno(fp) > sockfd ? fileno(fp) + 1 : sockfd + 1 ;

		if (select(maxfdp1, &rset, NULL, NULL, NULL) < 0) {
			printf("select error: %s\n", strerror(errno));
		}

		if (FD_ISSET(sockfd, &rset)) {
			if ((n = read(sockfd, buf, MAXLINE)) == 0) {
				if (stdineof == 1)
					break;
				else
					printf("server terminated\n");
			}

			if (write(fileno(stdout), buf, n) < 0) {
				printf("write error: %s\n", strerror(errno));
			}
			memset(buf, 0x00, MAXLINE);
		}

		if (FD_ISSET(fileno(fp), &rset)) {
			if ((n = read(fileno(fp), buf, MAXLINE)) == 0) {
				stdineof = 1;
				if (shutdown(sockfd, SHUT_WR) < 0) {
					printf("shutdown error: %s\n", strerror(errno));
				}
				FD_CLR(fileno(fp), &rset);
				break;
			}

			if (strcmp(quit_code, buf) == 0) {
				printf("client quit\n");
				break;
			}

			if (write(sockfd, buf, n) < 0) {
				printf("write error: %s\n", strerror(errno));
			}
			memset(buf, 0x00, MAXLINE);
		}
	}

	if ((write(sockfd, quit_code, strlen(quit_code))) < 0) {
		printf("tell server quit error\n");
	}
}

int main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;

	if (argc != 2) {
		printf("tcpcli1 <IPaddres>\n");
		return -1;
	}

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("socket error:%s\n", strerror(errno));
		return -1;
	}

	memset(&servaddr, 0x00, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) < 0) {
		printf("inet_pton error:%s\n", strerror(errno));
		return -1;
	}

	if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
		printf("connect error:%s\n", strerror(errno));
		return -1;
	}

	str_cli(stdin, sockfd);
	exit(0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值