首页 \ 问答 \ 导致Socket.Receive返回的最小字节数是多少?(What is the minimum number of bytes that will cause Socket.Receive to return?)

导致Socket.Receive返回的最小字节数是多少?(What is the minimum number of bytes that will cause Socket.Receive to return?)

我们正在使用一个应用程序协议,它指定前4个字节中消息的长度指示符。 Socket.Receive将返回与协议栈中当前或块一样多的数据,直到数据可用。 这就是为什么我们必须不断地从套接字读取,直到我们收到长度指示器中的字节数。 如果另一方关闭连接,Socket.Receive将返回0。 我明白了这一切。

是否有必须读取的最小字节数? 我问的原因是文档似乎完全有可能当socket.Receive可以返回时,整个长度指示符(4个字节)可能不可用。 然后我们必须继续努力。 最小化我们调用socket.receive的次数会更有效,因为它必须将内容复制到缓冲区中。 因此,获取长度指示符一次获得一个字节更安全,假设4个字节始终可用或我们是否应该继续尝试使用偏移量变量获得4个字节是否安全?

我认为可能存在某种默认最低级别的原因是我遇到了一个名为ReceiveLowWater变量的变量,我可以在套接字选项中设置它。 但这似乎只适用于BSD。 MSDN请参阅SO_RCVLOWAT。

它并不是那么重要,但我正在尝试编写单元测试。 我已经在界面后面包装了一个标准的.Net Socket。


We are using a application protocol which specifies the length indicator of the message in the first 4 bytes. Socket.Receive will return as much data as in the protocol stack at the time or block until data is available. This is why we have to continously read from the socket until we receive the number of bytes in the length indicator. The Socket.Receive will return 0 if the other side closed the connection. I understand all that.

Is there a minimum number of bytes that has to be read? The reason I ask is from the documentation it seems entirely possible that the entire length indicator (4 bytes) might not be available when socket.Receive can return. We would then have to have to keep trying. It would be more efficient to minimize the number of times we call socket.receive because it has to copy things in and out of buffers. So is it safer to get a single byte at a time to get the length indicator, is it safe to assume that 4 bytes will always be available or should we keep trying to get 4 bytes using an offset variable?

The reason that I think that there may be some sort of default minimum level is that I came across a varaible called ReceiveLowWater variable that I can set in the socket options. But this appears to only apply to BSD. MSDN See SO_RCVLOWAT.

It isn't really that important but I am trying to write unit tests. I have already wrapped a standard .Net Socket behind an interface.


原文:https://stackoverflow.com/questions/13087268
更新时间:2023-05-27 21:05

最满意答案

简单解决方案

A *a = new A;
shared_ptr<A> aPtr1(a);
{
    // construct new shared pointer from old one.
    shared_ptr<A> aPtr2(aPtr1);
}
aPtr1->foo();

如果您想要更复杂的内容,请参阅http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html

标头<boost/enable_shared_from_this.hpp>定义了类模板enable_shared_from_this 。 它用作基类,允许从成员函数中获取当前对象的shared_ptr


编辑:我应该提到enable_shared_from_this有一些不幸的派生问题。 但是,以下适用于c ++ 11; 我没有尝试使用提升,但我想它也应该在那里工作。 我认为这有点像黑客; 当你使用shared_ptr时使用原始指针必然会流下眼泪:

struct X : std::enable_shared_from_this {
  /* stuff */
};

struct Y : X {
  /* stuff */
  void yonly() {};
};

int main() {
  Y* y = new Y;
  shared_ptr<Y> sy(y);
  sy->yonly();
  {
    auto sy2 = std::shared_ptr<Y>(y->shared_from_this(), y);
    sy2->yonly();
  } 
  std::cout << "Block ended" << std::endl;
  return 0;
} 

Simple solution:

A *a = new A;
shared_ptr<A> aPtr1(a);
{
    // construct new shared pointer from old one.
    shared_ptr<A> aPtr2(aPtr1);
}
aPtr1->foo();

If you want something more complicated, see http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html :

The header <boost/enable_shared_from_this.hpp> defines the class template enable_shared_from_this. It is used as a base class that allows a shared_ptr to the current object to be obtained from within a member function.


Edit: I should have mentioned that enable_shared_from_this has some unfortunate issues with derivation. However, the following works with c++11; I didn't try it with boost but I suppose it should work there, too. I think it's a bit of a hack; using raw pointers when you use shared_ptr's is bound to end in tears:

struct X : std::enable_shared_from_this {
  /* stuff */
};

struct Y : X {
  /* stuff */
  void yonly() {};
};

int main() {
  Y* y = new Y;
  shared_ptr<Y> sy(y);
  sy->yonly();
  {
    auto sy2 = std::shared_ptr<Y>(y->shared_from_this(), y);
    sy2->yonly();
  } 
  std::cout << "Block ended" << std::endl;
  return 0;
} 

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)