尽管套接字已准备好读取,但读取系统调用返回零字节 [英] Read system call returning zero bytes though the socket is ready for read

查看:25
本文介绍了尽管套接字已准备好读取,但读取系统调用返回零字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c 中有一个客户端 - 服务器应用程序,它使用 unix 域套接字.

I have a client-server application in c which is using unix domain socket.

这里服务器正在从客户端读取一个整数.客户端成功向服务器发送 4 个字节.

Here server is reading a integer from client. Client successfully sends a 4 bytes to server.

但在服务器端我可以看到 read 系统调用正在从套接字读取 0 个字节.

but at the server end I could see that the read system call is reading 0 bytes from socket.

我非常确定数据可以在服务器上读取,因为读取操作之前的 select 调用成功.

I am pretty much sure that the data is available for read on server since the select call before read operation is succeeding.

知道为什么会这样吗?

推荐答案

没有任何代码,很难猜测到底发生了什么,但是 select() 成功并不一定意味着一些数据准备好了待读.

Without any code, it's hard to guess what exactly happens, but select() succeeding does not necessarily mean that some data are ready to be read.

select() 只是承诺如果您尝试 one 读取此文件描述符,您将不会被阻止.原因可能是以下任何一个:

select() just makes the promise that if you try one read from this file-descriptor you will not be blocked. The reason might be any of these:

  • 内核将一些要通过您读取的文件描述符传送的数据存储在内部缓冲区中(这正是您在这里所期望的),
  • 文件描述符的另一端被关闭,那么将永远不会传递新数据(您观察到的 EOF),
  • 文件描述符对于读取操作处于无效状态,任何读取尝试都将立即失败(读取后请参阅errno).
  • the kernel stores in an internal buffer some data to be delivered through the file-descriptor you read (it's what you are expecting here),
  • or the other end of the file-descriptor is closed, then no new data will ever be delivered (the EOF you observe),
  • or the file-descriptor is in an invalid state for a read operation and any read attempt will immediately fail (see errno after read).

这篇关于尽管套接字已准备好读取,但读取系统调用返回零字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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