send()是否不传送所有字节? [英] send() not deliver all bytes?

查看:79
本文介绍了send()是否不传送所有字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 winsock 中的 send()不能保证提供您请求的所有字节?

Why doesn't send() in winsock guarantee delivery of the all bytes you request?

这是TCP,它正在阻止套接字。

This is TCP and it's blocking sockets.

类似地,这种情况发生在非阻止时。

Similarly, this happens when non-blocking. How can you guarantee that you send everything?

我注意到 recv()也是一样。

推荐答案

如果未发送所有内容,只需再次致电 send 休息。如果阻止,则可以立即执行。如果是非阻塞状态,则可以等待或使用套接字发现方法(例如 select 或I / O完成端口)。 recv 也是如此。如果您没有得到想要的一切,请再次致电 recv 。这是 recv send 都返回发送或接收的字节数的原因之一。

If it didn't send everything, just call send again on the rest. If blocking, you can do it immediately. If non-blocking, you can either wait or use a socket discovery method (like select or I/O completion ports). The same goes for recv. If you didn't get all you wanted, call recv again. This is one of the reasons both recv and send return the number of bytes sent or received.

您传递给发送 recv 的字节数只是一个限制。它可以发送的数据少于此数(尽管除非进行非阻止,否则通常不会发送)。但它肯定能收到的收益少于那一点。 (操作系统无法控制它接收多少数据或何时接收它。)

The number of bytes you pass to send or recv is just a limit. It can send less than that (though, unless non-blocking, usually won't). But it can definitely receive less than that. (The OS has no control over how much data it receives or when it receives it.)

为您实现了TCP。但是,如果您具有涉及应用程序级消息的应用程序协议,则该应用程序必须实现它们。魔术不会发生。 TCP不会将字节粘合在一起给您。 TCP是字节流协议,而不是消息协议。如果需要消息,则必须实现它们。

TCP is implemented for you. But if you have an application protocol that involves application-level messages, then the application has to implement them. It won't happen by magic. TCP doesn't "glue the bytes together" into a message for you. TCP is a byte-stream protocol, not a message protocol. If you want messages, you have to implement them.

这篇关于send()是否不传送所有字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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