读取套接字中的缓冲区
如果read()的返回值与缓冲区大小相同,我如何读取其余信息?如果再次调用read,它会读取下一个数据块,还是从头开始读取,或者未读入缓冲区的信息会永久丢失吗?
If the return value of read() is the same as the buffer size, how do I read the rest of the information? If read is invoked again, will it read the next chunk of data, or read from beginning again, or is the information not read into the buffer permanently lost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 TCP (SOCK_STREAM) 套接字上,您可以获得下一个数据块。从 TCP 套接字读取类似于从文件读取。
在 UDP (SOCK_DGRAM) 数据报套接字上,如果缓冲区不足以消耗整个数据报,则会丢失数据报的其余部分。后续读取将消耗下一个到达的数据报。
On a TCP (SOCK_STREAM) socket, you get the next chunk of data. Reading from TCP sockets are analogous to reading from a file.
On a UDP (SOCK_DGRAM) datagram socket, if your buffer isn't sufficient to consume the entire datagram, you lose the rest of the datagram. A subsequent read will consume the next datagram that arrives.