借助C TCP套接字,可以“发送”返回零?
使用 TCP 套接字时,C send
函数是否有可能返回零?手册页只是说它将返回发送的字节数,但我不确定当它无法发送任何数据时它是否会返回-1。
Is it ever possible for the C send
function to return zero when using TCP sockets? The man page just says that it will return the number of bytes sent, but I am not sure if it will just return -1 when it can't send any data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我很确定,虽然记忆深处在时间的迷雾中,但我以前见过它在另一端跟不上的大量数据传输的情况下返回零。
根据记忆,在这种情况下,远程 TCP 堆栈缓冲区已填满,堆栈已通知本地端要延迟,直到清除了一些空间并且本地缓冲区也已填满。
此时,从技术上讲,这并不是一个错误(因此没有返回 -1),但本地堆栈无法接受任何数据。
我不完全确定现在的情况是这样,因为当前的 Posix 标准似乎表明在这种情况下它将简单地阻塞(或者如果设置为非阻塞则失败)。
然而,我怀疑这是一个有争议的问题。您确实有可能返回少于您请求发送的字节,因此您应该有适当的代码来处理这个问题。
而且,由于处理“比您请求的少一”的逻辑与处理“零字节”的逻辑几乎相同,因此您也可以假设它可以返回零。
I'm pretty certain, though the memory is deep in the mists of time, that I've seen it return zero before, in the situation of massive data transfers where the other end was not keeping up.
From memory, in that case, the remote TCP stack buffers had filled up, the stack had notified the local end that it was to delay until some space was cleared out and the local buffers had filled up as well.
At that point, it's not technically an error (hence no -1 returned) but no data could be accepted by the local stack.
I'm not entirely certain that's the case now since the current Posix standard seems to indicate it will simply block in that case (or fail if it's set up for non-blocking).
However, I suspect it's a moot point. You do have the possibility that it will return less than the bytes you requested to send and you therefore should have code in place to handle that.
And, since it will be pretty much the same logic handling 'one less than what you requested' as handling 'zero bytes', you may as well assume it can return zero.
好吧,总是存在这样的情况:您传入零作为要发送的字节数...在这种情况下,“返回发送的字节数”将表明它应该返回零字节。
无论如何,最好正确处理返回零的情况;它不会造成伤害,而且可能会有所帮助。
Well, there is always the case where you passed in zero as the number of bytes to send... in that case, "returning the number of bytes sent" would indicate that it should return zero bytes.
Probably best to handle the returns-zero case properly anyway; it can't hurt, and it might help.
这个问题的答案很可能取决于实现,因此根据操作系统的不同而有所不同。
当您请求传输 0 字节时,预期会出现 0 的一种情况。
The answer to this may well be implementation dependent and therefore vary based on the operating system.
One circumstance where 0 would be expected, when you request a transmission of 0 bytes.
BSD man 页面指出:
Posix 规范更进一步指出,在阻塞模式下,除非发生中断,否则所有数据都会被传输。
在这两种情况下,除非提供的计数为零,否则无法返回零。
The BSD man page states:
The Posix specification goes further and states that in blocking mode all data is transferred, unless an interrupt occurs.
In both cases zero cannot be returned unless the count supplied was zero.
我现在确实观察到
AF_UNIX
类型套接字上的send(2)
返回零。是的,这是由于
size
字段的值为零。所以,JFYI。
I do observe a zero return from the
send(2)
on anAF_UNIX
type socket right now.Yepp, it was due to the
size
field of zero value.So, JFYI.
send
当您send(socket, buf, 0, 0)
时返回 0我想提供简单的代码供其他人测试。
服务器:
在一个终端中:
nc -l localhost 10086
客户端:
在另一个终端中:
send
returns 0 when yousend(socket, buf, 0, 0)
I'd like to provide simple code for others to test.
Server:
In one terminal:
nc -l localhost 10086
Client:
In another terminal: