epoll:区分“监听者” FD
如何区分“侦听器”文件描述符和“客户端”文件描述符?
这是我在 manpage 示例中看到的内容:
if(events[n].data.fd == listener) {
...
} else {
...
}
'但是如果我没有怎么办访问监听器
?
抱歉,如果这是一个模糊的问题。我不太确定如何措辞。
How can I distinguish between "listener" file descriptors and "client" file descriptors?
Here's what I saw in the manpage example:
if(events[n].data.fd == listener) {
...
} else {
...
}
'But what if I don't have access to listener
?
Sorry if this is a vague question. I'm not quite sure how to word it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您正在编写一个服务器,您应该将侦听套接字描述符保留在某个变量中(手册页中的listener),或者为您提供给epoll_ctl(的每个套接字)设置一个小结构2) 并使用
struct epoll_event
的data.ptr
成员指向它(不要忘记在套接字关闭时取消分配该结构)。像这样的事情:
正如您所看到的,它比仅仅访问服务器套接字描述符要做更多的工作,但它有一个很好的特性,可以将与一个套接字相关的所有信息保存在一处。
Assuming you are writing a server, you should either keep the listening socket descriptor around in some variable (
listener
in the manual page), or setup a small structure for each socket you give toepoll_ctl(2)
and point to it withdata.ptr
member of thestruct epoll_event
(don't forget to de-allocate that structure when socket is closed).Something like this:
As you can see it's much more work then just having access to the server socket descriptor, but it has a nice property of keeping all information related to one socket in one place.