user4918159
user4918159

Reputation: 376

will poll() return immediately for UDP socket if there are data not read from last read()?

An existing program is written this way for UDP socket (in blocking mode):

while (true) {
    poll();
    if (POLLIN is set) {
        read(fd, buf, bufSize);
    }
}

For UDP, each read() read 1 and only 1 datagram (packet). If there are multiple packets available in the socket recv buf, the above code reads 1 packet only at each read(). My question is: will the next poll() return immediately, thus the above code still can read from socket very quickly? Or could next poll() wait until there is new packet arrives on the socket thus the code effectively falls behind in reading?

The doc seem to suggest that next poll() will return immediately as long as there are data in the buffer. But the code seem to fall behind in reading, and I don't know the cause is in the above code or somewhere else.

The preferred way is likely:

set the socket to non blocking
read in the loop until errno = EWOULDBLOCK or EAGAIN

Thanks.

Upvotes: 2

Views: 403

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595320

If there is already data in the socket buffer when poll() is called, it should signal POLLIN immediately if POLLIN is requested, yes. It should not wait for the next packet to arrive in the buffer before signaling POLLIN.

Upvotes: 3

Related Questions