当另一方写入并关闭时,epoll 控制的非阻塞套接字上丢失字节
我当前订阅了一个非阻塞套接字:
ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP| EPOLLET;
它接收了几个 EPOLLIN,我以非阻塞方式读取这些 EPOLLIN,直到 EAGAIN,然后我收到 HUP 和 EAGAIN。 RDHUP,有时需要多读取几个字节。
另一面是:
send(socket,960_bytes_buffer)
close(socket);
我已经在 epollin 和 close time 的事件循环中直接尝试使用 msg_peek 进行接收,并添加接收到的数据,但它并不总是收到 960 个,有时只收到大约 480 个字节。
使套接字非阻塞或在发送和关闭之间在客户端中放置 sleep(1) 可以正常工作。
在我看来,这更多是非阻塞套接字的问题,而不是 epoll 相关的问题。像“nc -l -p port”这样简单的东西会接收适当数量的字节。
I've a non-blocking socket currently subscribed to:
ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP| EPOLLET;
It receives a couple of EPOLLINs which I read non-blocking till EAGAIN and then I receive HUP & RDHUP, sometimes with a few bytes more to read.
The other side is just:
send(socket,960_bytes_buffer)
close(socket);
I've tried recv with msg_peek directly in the event loop for both epollin and in close time, and adding received data it doesn't receive 960 always, sometimes only around 480 bytes.
Making the socket non-blocking or putting a sleep(1) in the client between the send and the close works OK.
It looks to me more a problem of non-blocking sockets than epoll related. Something simple as "nc -l -p port" receives the proper amount of bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看最终的SO_LINGER页面,或者:为什么我的tcp不可靠,它很好地解释了发生了什么以及如何解决它。
Have a look at The ultimate SO_LINGER page, or: why is my tcp not reliable which nicely explains what is happening and how to fix it.