从套接字读取是等待还是得到 EOF?
Does reading from a socket wait or get EOF?
我正在用 C 实现客户端和服务器之间的简单连接。
在客户端,我处于循环中,从文件中读取;每次 BUFFER_SIZE 字节并发送到服务器端(没有上传错误处理)。
//client side
bytesNumInput = read(inputFileFD,bufInput,BUFFER_SIZE)
bytesSend = write(sockfd,bufInput,bytesNumInput)
当然服务器也在循环
//server side
bytesRecv = read(sockfd,bufOutput,bytesNumInput)
现在,我的问题是:
- 如果服务器读取速度比客户端快,我可以在连接中间获取 EOF 吗?
- 读取函数是等待获取所有数据还是与从文件读取相同?
- 服务器是否有可能在 1 次写入迭代中处理 2 次读取迭代?
- 不,服务器将等待传入数据。套接字提供控制流。
- 问题我不清楚,阅读总是尝试获取所有请求的数据,但如果没有那么多那么它会获取可用的
- 是的,套接字没有消息语义,只有字节流。
Can I get EOF in the middle of the connection if the server reads faster than the client?
没有。 EOF 表示对等方已断开连接。如果连接仍然存在,read()
将阻塞直到 (a) 至少传输了一个字节,(b) 发生 EOF,或 (c) 发生错误。
Does the read function wait to get all the data or is it the same as reading from a file?
见上文 (a)。
Is it possible the server will handle 2 read in 1 write iteration?
是的。 TCP 是 byte-stream 协议,而不是消息传递协议。
我正在用 C 实现客户端和服务器之间的简单连接。 在客户端,我处于循环中,从文件中读取;每次 BUFFER_SIZE 字节并发送到服务器端(没有上传错误处理)。
//client side
bytesNumInput = read(inputFileFD,bufInput,BUFFER_SIZE)
bytesSend = write(sockfd,bufInput,bytesNumInput)
当然服务器也在循环
//server side
bytesRecv = read(sockfd,bufOutput,bytesNumInput)
现在,我的问题是:
- 如果服务器读取速度比客户端快,我可以在连接中间获取 EOF 吗?
- 读取函数是等待获取所有数据还是与从文件读取相同?
- 服务器是否有可能在 1 次写入迭代中处理 2 次读取迭代?
- 不,服务器将等待传入数据。套接字提供控制流。
- 问题我不清楚,阅读总是尝试获取所有请求的数据,但如果没有那么多那么它会获取可用的
- 是的,套接字没有消息语义,只有字节流。
Can I get EOF in the middle of the connection if the server reads faster than the client?
没有。 EOF 表示对等方已断开连接。如果连接仍然存在,read()
将阻塞直到 (a) 至少传输了一个字节,(b) 发生 EOF,或 (c) 发生错误。
Does the read function wait to get all the data or is it the same as reading from a file?
见上文 (a)。
Is it possible the server will handle 2 read in 1 write iteration?
是的。 TCP 是 byte-stream 协议,而不是消息传递协议。