epoll监听文件_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?

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().

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

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.

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


1 - Create all your server sockets

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

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

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.

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.

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业正在做毕设学生与需要项目实战练习Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页