使用 C TCP 套接字,“发送"可以返回零吗? [英] With C TCP sockets, can 'send' return zero?

查看:36
本文介绍了使用 C TCP 套接字,“发送"可以返回零吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 TCP 套接字时,C send 函数是否有可能返回零?手册页只是说它将返回发送的字节数,但我不确定它在无法发送任何数据时是否只返回-1.

解决方案

我很确定,虽然记忆在时间的迷雾中很深,但我以前见过它在海量数据的情况下归零在另一端没有跟上的情况下转移.

从内存来看,在这种情况下,远程 TCP 堆栈缓冲区已填满,堆栈已通知本地端延迟,直到一些空间被清除并且本地缓冲区也已填满.

此时,从技术上讲,这不是错误(因此没有返回 -1),但本地堆栈无法接受任何数据.

我不完全确定现在是这种情况,因为当前的 Posix 标准似乎表明它会在这种情况下简单地阻塞(或者如果它设置为非阻塞则失败).>

但是,我怀疑这是一个有争议的问题.您确实有可能返回少于您请求发送的字节数,因此您应该有适当的代码来处理.

而且,由于处理比您请求的少一个"与处理零字节"的逻辑几乎相同,因此您不妨假设它可以返回零.

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.

解决方案

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.

这篇关于使用 C TCP 套接字,“发送"可以返回零吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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