2011-06-16 150 views
1

我是網絡編程的新手,我聽說過epoll。我閱讀了幾個教程,現在我對epoll的功能以及如何實現它有了一些基本的瞭解。epoll可以接收udp數據包嗎?

問題是,即使客戶端使用udp連接,我也可以使用epoll嗎?我讀的所有教程都使用tcp連接。

還有一個很好的教程或示例代碼,解釋使用epoll的基於多線程的服務器實現嗎?我從網上獲得的這些教程僅展示瞭如何在單線程上創建簡單的回顯服務器。

在此先感謝。

+0

你可以使用任何種類的epoll套接字。 – 2011-06-16 08:51:22

回答

1

使用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在每個系統中都不可用,並且使用這種類型的庫,您的程序更具可移植性。

Here你可以找到一個libev使用UDP的例子,Here另一個使用libevent的例子。