首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >epoll可以接收udp数据包吗?

epoll可以接收udp数据包吗?
EN

Stack Overflow用户
提问于 2011-06-16 16:37:51
回答 1查看 5K关注 0票数 1

我是网络编程的新手,听说过epoll。我读了几个教程,现在我对epoll的作用以及如何实现有了一些基本的了解。

问题是,即使客户端将使用udp连接,我也可以使用epoll吗?我读过的所有教程都使用tcp连接。

另外,有没有很好的教程或示例代码来解释使用epoll的基于多线程的服务器实现?我从网上得到的那些教程只展示了如何在单线程上创建一个简单的echo服务器。

提前谢谢。

EN

Stack Overflow用户

回答已采纳

发布于 2011-06-16 17:00:42

将epoll与UDP一起使用是没有问题的,epoll只是通知文件描述符中是否有要读取的数据。在读/写中有一些暗示...与UDP套接字行为相关的操作(来自epoll的手册页):

For stream-oriented files (e.g., pipe, FIFO, stream socket), the condition that the read/write I/O space is exhausted can also be detected by checking the amount of data read from / written to the target file descriptor. For example, if you call read(2) by asking to read a certain amount of data and read(2) returns a lower number of bytes, you can be sure of having exhausted the read I/O space for the file descriptor. The same is true when writing using write(2). (Avoid this latter technique if you cannot guarantee that the monitored file descriptor always refers to a stream-oriented file.)

另一方面,直接使用epoll并不是很常见。使用epoll的最佳方式是使用事件循环库,例如libev或libevent。这是一个更好的方法,因为epoll并不是在每个系统中都可用,并且使用这种库你的程序更容易移植。

您可以找到libev与UDP一起使用的示例,以及Here与libevent.一起使用的其他示例。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6369142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档