epoll异常处理 [英] epoll exception handle

查看:148
本文介绍了epoll异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用epoll进行网络编程。我想知道一种情况,服务器没有收到客户端发送的所有数据。例如,如果客户端发送100字节,服务器只接收94字节。如何处理这种情况?



提前感谢。

解决方案

>准备,它不提供任何关于数据量的保证。 EPOLLIN只给你保证下一个对描述符的读操作不会被阻塞,并且至少会读取1个字节。

由于通常将描述符设置为非阻塞的各种(部分os-具体)原因,通常的成语是读取,直到EAGAIN返回。如果这是少于预期的数据(例如,如果您有一个消息带有我的大小是100字节的标题),那么您将等待下一个EPOLLIN(或EPOLLHUP)并重复(或中止)。 / p>

对于TCP,收到少于预期的数据是绝对正常的情况。重复。



使用UDP,除非你提供一个太小的缓冲区(这将丢弃剩余部分!),这不会发生。永远不能。 UDP一次传送整个数据报或什么也不传送。没有部分交货。如果发生IP分片,UDP将把所有分片重新组合成一个数据报,并传送整个数据报。如果一个片段丢失,UDP将不传递任何内容。


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..

解决方案

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.

这篇关于epoll异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆