Linux select 实现聊天室

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
	if (argc <= 2)
	{
		printf("usage: %s ip_address port_number\n", basename(argv[0]));
		return 1;
	}
	const char* ip = argv[1];
	int port = atoi(argv[2]);
	printf("ip is %s and port is %d\n", ip, port);

	int ret = 0;
	struct sockaddr_in address;
	bzero(&address, sizeof(address));
	address.sin_family = AF_INET;
	inet_pton(AF_INET, ip, &address.sin_addr);
	address.sin_port = htons(port);

	int listenfd = socket(PF_INET, SOCK_STREAM, 0);
	assert(listenfd >= 0);

	ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
	assert(ret != -1);

	ret = listen(listenfd, 5);
	assert(ret != -1);

	char buf[1024];
	fd_set read_fds;
	fd_set allset;

	FD_ZERO(&read_fds);
	FD_ZERO(&allset);

	FD_SET(listenfd, &allset);

	int maxfd = listenfd;

	
	while (1)
	{
		read_fds = allset;	//  更新事件

		memset(buf, '\0', sizeof(buf));
		ret = select(maxfd + 1, &read_fds, NULL, NULL, NULL);

		printf("select one\n");
		if (ret < 0)
		{
			printf("selection failure\n");
			break;
		}

		for (int myfd = listenfd; myfd <= maxfd; myfd++)
		{
			if (FD_ISSET(myfd, &read_fds))
			{

				if (myfd == listenfd) {
					struct sockaddr_in client_address;
					socklen_t client_addrlength = sizeof(client_address);

					int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
					if (connfd < 0)
					{
						printf("errno is: %d\n", errno);
						close(listenfd);
					}
					int nReuseAddr = 1;
					setsockopt(connfd, SOL_SOCKET, SO_OOBINLINE, &nReuseAddr, sizeof(nReuseAddr));

					FD_SET(connfd, &allset);
					if (connfd > maxfd)
						maxfd = connfd;
					char remote_addr[INET_ADDRSTRLEN];
					printf("connected with ip: %s and port: %d\n", inet_ntop(AF_INET, &client_address.sin_addr, remote_addr, INET_ADDRSTRLEN), ntohs(client_address.sin_port));

				}
				else {
					ret = recv(myfd, buf, sizeof(buf) - 1, 0);
					if (ret <= 0)
					{
						close(myfd);
						FD_CLR(myfd, &allset);
						break;
					}
					printf("get %d bytes of normal data: %s\n", ret, buf);
				}
			}

		}

	}

	close(listenfd);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DongGu.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值