epoll的 - 轮询多个文件描述符(即是插座) [英] epoll - polling multiple file descriptors (that are sockets)

查看:379
本文介绍了epoll的 - 轮询多个文件描述符(即是插座)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了手册页和通过这个例子去了用法:的 https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/ ,但我想不出一个这样做,我尝试使用epoll的做的,任何人都可以借给我一些帮助?

I read the man page and went through this example for usage: https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/, but I can't figure a way to do what I am trying to do using epoll, can anyone lend me some help?

基本上我写的netcat型工具来更好地学习C /联网。我创建了一个基本的服务器/客户端,可以聆听并连接一个端口上。现在我想展开服务器方面监听端口范围。

Basically I am writing a netcat type utility to better learn C/networking. I have created a basic server/client that can listen and connect on one port. Now I want to expand the server aspect to listen on a port range.

我打算通过一个循环为每个端口创建插座(),绑定()来做到这一点,听()。

I plan to do this via a for loop for each port creating socket(), bind(), and listen().

由于端口范围可以达到65K,我需要轮询他们和accept()当一个人有收到连接请求。

Since port range can be up to 65k, I need to poll them and accept() when one has a connection request received.

Unlinke使用投票或epoll的处理多个连接的一个端口上典型的C系列服务器,这是几千年潜力的港口之一的一个连接。我不能完全从例子中我怎么能EPOLL插座,当请求连接接受它,并取消绑定/停止监听/查询得到。

Unlinke typical C servers that use poll or epoll to deal with multiple connections on one port, this is one connection on one of several thousand potential ports. I can't quite get from the examples how I could epoll the sockets and when a connection is requested accept it and unbind/stop listening/polling.

有没有人有任何的例子说明/ code让我开始吗?

Has anyone got any example explanations/code to get me started please?

推荐答案

1 - 创建所有的服务器套接字

1 - Create all your server sockets

2 - 及其文件描述符将包含在一个范围内,像[4,199]

2 - Their file descriptors will be contained in a range, something like [4, 199]

3 - 所有的人都加入到epoll的描述,在一个循环

3 - Add all of them to the epoll descriptor, in a loop.

然后在正常epoll的循环,如果与该事件的描述是[4199]或无论是你在练习场的范围,那么你就知道这是一个服务器套接字,你需要接受它,否则做什么你与你的连接描述符。

Then in the normal epoll loop, if the descriptor with the event is in the range of [4,199] or whatever was your range in practice, then you know it's a server socket and you need to accept on it, otherwise do whatever you do with your connected descriptor.

做的另一种方法是创建2 epoll的描述,并使用一个只为服务器插槽和其他只为连接描述符。这样做的振作的是,你需要对它们都使用epoll的话!所以我不建议这一点。

An alternative way of doing is to create 2 epoll descriptors, and use one just for the server sockets and the other just for the connected descriptors. The perk of this is that you'd need to use epoll on them both then! So I wouldn't advise this.

由于您要创建描述符的数量是如此之大,你很可能需要修改一些内核参数,因为你可能会打一些限制与流程中这么多打开的描述符。

Since the number of descriptors you want to create is so big, you will most likely need to change some kernel parameter because you will probably hit some limit with so many open descriptors in a process.

这篇关于epoll的 - 轮询多个文件描述符(即是插座)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆