epoll可以接收udp数据包吗?
问题描述:
我是网络编程的新手,我听说过epoll。我阅读了几个教程,现在我对epoll的功能以及如何实现它有了一些基本的了解。epoll可以接收udp数据包吗?
问题是,即使客户端使用udp连接,我也可以使用epoll吗?我读的所有教程都使用tcp连接。
还有一个很好的教程或示例代码,解释使用epoll的基于多线程的服务器实现吗?我从网上获得的这些教程仅展示了如何在单线程上创建简单的回显服务器。
在此先感谢。
答
使用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在每个系统中都不可用,并且使用这种类型的库,您的程序更具可移植性。
你可以使用任何种类的epoll套接字。 – 2011-06-16 08:51:22