多路复用

本文介绍了一个使用C语言实现的Socket通信程序,通过Select函数实现了多路复用,能够同时处理多个客户端的连接请求。程序首先创建监听套接字并绑定地址,然后开始监听连接。当有新的客户端连接时,程序会接受连接并将其文件描述符加入到监控的文件描述符集中。之后,程序不断循环,使用Select函数检查哪些套接字已准备好读取或写入数据,并调用相应的处理函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


#define SIZE 1024
#define PORT 9999

// sfd 用于和客户端通信
void handl_client(int sfd, fd_set *allset)
{
	char buf[SIZE] = {0};
	int ret = read (sfd, buf, SIZE-1);
	if (ret == -1)
	{
		perror ("read");
		return;
	}
	if (ret == 0)
	{
		printf ("客户端退出\n");
		FD_CLR(sfd, allset);
		return;
	}
	buf[ret] = '\0';
	printf ("read %d bytes : %s\n", ret, buf);
	
	int i; 
	for (i = 0; i < ret - 1; i++)
	{
		buf[i] = buf[i] + 'A' - 'a';
	}
	
	ret = write (sfd, buf, ret);
}

// 监听套接字
int init()
{
	int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == listen_socket)
	{
		perror("创建套接字失败");
		return -1;
	}
	
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(struct sockaddr_in));
	addr.sin_family      = AF_INET;             /* Internet地址族 */
	addr.sin_port        = htons(PORT);         /* 端口号 */
	addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* IP地址, 绑定本地的所有ip地址*/

	int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
	if(-1 == ret)
	{
		perror("绑定失败");
		return -1;
	}
	
	// 3、监听套接字
	ret = listen(listen_socket, 5);
	if(-1 == ret)
	{
		perror("监听失败");
		return -1;
	}
	
	return listen_socket;
}

// 通信套接字
int myAccept(int listen_socket)
{
	struct sockaddr_in client_addr;
	socklen_t  len = sizeof(client_addr);
	int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
	if (-1 == client_socket)
	{
		perror("accept  失败");
		return -1;
	}
	
	printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
	
	
	return client_socket;
}


int main()
{
	// 初始化套接字
	int listen_fd = init();
	if (listen_fd == -1)
		return -1;
	

	int maxfd = listen_fd;
	fd_set allset;
	fd_set rset;
	
	FD_ZERO(&allset);              // 将文件描述符集清空 
	FD_SET(listen_fd, &allset);    // 将监听文件描述符加入到监听序列
	
	int client_fd[FD_SETSIZE];     // 代表select能够检测客户端数量
	
	// 对数组进行初始化,全部置为-1,凡是值为-1的位置,都是空位置
	int i;
	for (i = 0; i < FD_SETSIZE; i++)
	{
		client_fd[i] = -1;
	}
	
	while (1)
	{
		rset = allset;
		int count = select(maxfd + 1, &rset, NULL, NULL, NULL);
		if (count == -1)
		{
			perror ("select");
			break;
		}
		
		if (FD_ISSET(listen_fd, &rset))  // 查看是否有客户端要连接
		{
			int conn_fd = myAccept(listen_fd);
			if (conn_fd == -1)
			{
				continue;
			}
			
			// 找个空位置存与客户端通信的文件描述符
			for (i = 0; i < FD_SETSIZE; i++)
			{
				if (client_fd[i] == -1)     // 找到一个空位置
				{
					client_fd[i] = conn_fd;
					break;
				}
			}
			
			// 判断文件描述符集是否已达上限
			if (i == FD_SETSIZE)
			{
				close (conn_fd);
			}
			else
			{
				FD_SET(conn_fd, &allset);
				if (maxfd < conn_fd)
					maxfd = conn_fd;
			}
			
			if (--count <= 0)
				continue;
		}
		
		for (i = 0; i < FD_SETSIZE; i++)
		{
			if (client_fd[i] == -1)
				continue;
			
			if (FD_ISSET(client_fd[i], &rset))
			{
				handl_client(client_fd[i], &allset);
				if (--count <= 0)
					break;
			}
		}
	}
	
	close (listen_fd);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值