epoll exception handle
Asked Answered
G

1

1

I am working on a network programming using epoll. I was wondering a situation that server didn't receive all the data client sent. For example if a client sent 100 bytes and somehow server only received 94bytes. How do I handle this case?

Thanks in advance..

Gagger answered 21/6, 2011 at 7:1 Comment(3)
This has nothing to do whatsoever with either C++ or epoll.Tel
normally this has to be handled at the application level, if you receive less data than expected, you need to re-request that data somehow. If your connection is TCP, I'm surprised, if UDP, well anything is possible. It could be that a subsequent wait operation will return the outstanding data - have you tried that?Bearnard
In short its not an epoll issue. Are you talking about partial reads ? In that case read returns the number of bytes read & you know that message is 100 bytes (from message header protocol) than you read again till you desired size and form the message from e.g. 94 + 6 bytes from next read.Tied
S
3

An epoll signals readiness, it does not give any guarantees about the amount of data. EPOLLIN only gives to you the guarantee that the next read operation on the descriptor will not block and will at least read 1 byte.
As one normally sets descriptors to non-blocking for a variety of (partly os-specific) reasons, the usual idiom is to read until EAGAIN is returned. If that is less data than was expected (for example if you have a message with a header that says "my size is 100 bytes"), then you will wait for the next EPOLLIN (or EPOLLHUP) and repeat (or abort).

For TCP, recieving less data than expected is an absolutely normal condition. Repeat.

With UDP, unless you supply a too small buffer (this will discard the remainder!), this will not happen. Never, ever. UDP delivers an entire datagram at a time or nothing. There are no partial deliveries. If IP fragmentation occurs, UDP will reassemble all fragments into one datagram and deliver a whole datagram. If a fragment was lost, UDP will deliver nothing.

Siliqua answered 21/6, 2011 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.