user800799
user800799

Reputation: 3013

Can epoll receive udp packet?

I am a newbie of network programming and I've hear about epoll. I read a couple of tutorials and now I got some basic idea of what epoll does and how I can implement this.

The question is that can I use epoll even if client will using udp connection? All the tutorials I read used tcp connection.

Also is there a good tutorials or a sample code that explains multi-thread based server implementation using epoll? Those tutorials I got from online only showed how to create a simple echo server on single thread.

Thanks in advance.

Upvotes: 1

Views: 6081

Answers (1)

There is no problem to use epoll with UDP, the epoll just notifies if there is any data to read in the file descriptor. There are some implications in the read/write... operations related to the UDP socket behaviour (from the man page of 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.)

On the other hand is not very usual to use the epoll directly. The best way of using epoll is using an event loop library, libev, or libevent, for example. This is a better aproach, beacause epoll is not available in every system and using this kind of libraries your programs are more portable.

Here you can found an example of libev use with UDP, and Here other example with libevent.

Upvotes: 1

Related Questions