poll服务器

本文介绍了一个使用Poll进行事件监听的简易服务端程序实现过程。该程序通过socket创建连接,并利用Poll机制处理客户端请求,包括接收客户端消息并响应固定内容。文章展示了如何设置socket、处理客户端连接及读写操作。

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define array_size 1024
int startup(const char* _ip, int _port)
{
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock < 0)
	{
		perror("create sock is fail");
		return 2;
	}

	struct sockaddr_in peer;
	peer.sin_family = AF_INET;
	peer.sin_port = htons(_port);
	peer.sin_addr.s_addr = inet_addr(_ip);

	int opt = 1;
	if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
	{
		perror("setsockopt");
		return 3;
	}

	if(bind(sock, (struct sockaddr*)&peer, sizeof(peer)) < 0)
	{
		perror("bind");
		return 4;
	}

	if(listen(sock, 10) < 0)
	{
		perror("listen");
		return 5;
	}
	
	return sock;

}

static void Usage(const char* proc)
{
	printf("%s[ip][port]\n", proc);
}

int main(int argc, char *argv[])
{
	if(argc != 3)
	{
		Usage(argv[0]);
		return 1;
	}

	int listensock = startup(argv[1], atoi(argv[2]));

	struct  pollfd array_fds[1024];
	array_fds[0].fd = listensock;
	array_fds[0].events = POLLIN;

	int i = 1;
	for(; i < array_size; i++)
	{
		array_fds[i].fd = -1;
	}

	int _timeout = 1000;
	while(1)
	{
		switch(poll(array_fds, 1024, _timeout))
		{
			case 0:
				printf("timeout......");
				break;
			case -1:
				perror("poll");
				break;
			default:
				{
					int j = 0;
					for(; j < array_size; j++)
					{
						if(j == 0 && array_fds[0].revents & POLLIN)
						{
							struct sockaddr_in client;
							socklen_t len = sizeof(client);
							int new_fd = accept(array_fds[j].fd, (struct sockaddr*)&client, &len);
							if(new_fd < 0)
							{
								perror("new_fd: accept");
								continue;
							}else
							{
								printf("connect is success! ip: %s port: %d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
								
								int k = 1;
								for(; k < array_size; k++)
								{
									if(array_fds[k].fd < 0)
									{
										array_fds[k].fd = new_fd;
										array_fds[k].events = POLLIN;
										break;
									}
								}
								if(k == array_size)
								{
									printf("连接超出最大限度");
									return;
								}
							}
						}
						else if(j != 0 && array_fds[j].revents & POLLIN)
						{
							char buf[1024];
							memset(buf, 0, sizeof(buf));
							ssize_t s = read(array_fds[j].fd, buf, sizeof(buf)-1);
							if(s > 0)
							{
								buf[s-1] = '\0';
								printf("client say# %s\n", buf);
								array_fds[j].events = POLLOUT;
							}else if(s == 0)
							{
								printf("client is quit!");
								close(array_fds[j].fd);
								array_fds[j].fd = -1;
							}else
							{
								perror("read");
								close(array_fds[j].fd);
								array_fds[j].fd = -1;
							}
						}
						else if(j != 0 && array_fds[j].revents & POLLOUT)
						{
							const char* msg = "HTTP/1.1 200 OK \r\n\r\n

hello poll

"; write(array_fds[j].fd, msg, strlen(msg)); close(array_fds[j].fd); array_fds[j].fd = -1; } } } break; } } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值