导致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
最满意答案
简单解决方案
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 templateenable_shared_from_this
. It is used as a base class that allows ashared_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; }
相关问答
更多-
从boost :: shared_ptr转换为std :: shared_ptr?(Conversion from boost::shared_ptr to std::shared_ptr?)[2022-03-17]
你可以通过使用析构函数来执行std :: shared_ptr中的boost :: shared_ptr“内部”来携带引用: templatevoid do_release(typename boost::shared_ptr const&, T*) { } template typename std::shared_ptr to_std(typename boost::shared_ptr const& p) { return ... -
基本上你是对的。 您的示例将导致悬空指针(请注意,如果您使用boost::enable_shared_from_this作为基类,则会有一些例外)。 说明 问题 boost:shared_ptr和std::shared_ptr共享相同的想法:创建一个带有来自原始指针的引用计数的智能指针。 但是,它们也会遇到所有智能指针都有的相同问题:如果你在另一个与你的其他智能指针无关的智能指针中使用原始指针,你将以悬空指针和多次delete调用结束: int * ptr = new int; { std::sha ...
-
一个不同的shared_ptr实例的要点是尽可能保证(只要这个shared_ptr在范围内),它指向的对象将仍然存在,因为它的引用计数将至少为1。 Class::only_work_with_sp(boost::shared_ptr
sp) { // sp points to an object that cannot be destroyed during this function } 因此,通过使用对shared_ptr的引用,您将禁用该保证。 所以在你的第二种情况: Class: ... -
例如,全部或只有部分可以: 这取决于你想要完成的事情。 为什么向量包含shared_ptr而不是直接按值存储Person ? (你考虑过boost::ptr_vector吗?) 您还应该考虑一下,您真正应该weak_ptr是weak_ptr 。 如果我以后想要更改实现以使用johns_very_own_shared_ptr而不是boost类型,我是否被困在旧的实现中? 差不多,但修复并非不可能。 (我怀疑在C ++ 0x中,自由使用auto关键字会使这更容易处理,因为你不必修改调用代码,即使它没有使用typ ...
-
我理解,因为pc是模板参数,它被认为是shared_ptr类型的一部分,而不是cv限定。 是的,这被称为“不可推翻的背景”。 以下工作 boost::atomic_store(&const_p, boost::shared_ptr
(p)); 但是,它会创建一个额外的不必要的boost::shared_ptr 。 我的理解是对于boost::const_pointer_cast (p)也是如此。如果不再需要p,可以避免这种情况。 好吧,惊喜,你总是得到副本: templ ... -
我的直接反应(我承认,它不过是这样),听起来好像你正试图获得某种池分配器的效果。 你可能会更好地重载operator new和operator delete以更直接地获得你想要的效果。 有了这样的东西,你可以像普通的一样使用shared_ptr ,而你想要延迟的其他工作,将在该类的operator delete处理。 这留下了一个更基本的问题:你真的想用这个完成什么? 从内存管理的角度来看,一个共同的愿望是一次为大量对象分配内存,在整个块为空之后,立即释放整个块。 如果您尝试按照该顺序进行操作,则通过重载n ...
-
boost :: optional是否会触发shared_ptr上的引用计数?(Does boost::optional trigger a ref count on shared_ptr?)[2023-11-09]
是的,它必须。 boost::optional存储一个副本,因此对于shared_ptr ,这意味着有一个shared_ptr的副本,这意味着必须增加引用计数。 请注意,只要boost::optional为空,即它不包含shared_ptr的值,就没有对象的引用计数被填充。 换句话说,一个空的boost::optional不包含(空或其他) shared_ptr 。 请求的“语义”无法真正起作用,因为您在地图中保留了一个shared_ptr并返回了shared_ptr 。 但是,您可以返回boost::op ... -
简单解决方案 A *a = new A; shared_ptr aPtr1(a); { // construct new shared pointer from old one. shared_ptr aPtr2(aPtr1); } aPtr1->foo(); 如果您想要更复杂的内容,请参阅http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html : 标头是的,它可以达到2。 它可能会达到高于 2,因为我们不知道vector实现可以在内部推送多少副本。 允许复制任意次数。 Yes, it could reach 2. It could reach higher than 2 because we don't know how many copies the vector implementation may make internally as it's pushing back. It's allowed to copy as many times as ...您可以从enable_shared_from_this派生,然后您可以使用“ shared_from_this ()”而不是“this”来生成一个共享指针到您自己的对象。 链接示例: #include
class Y: public boost::enable_shared_from_this { public: shared_ptr f() { return shared_from_th ... 相关文章
更多- Java Number类
- Guava处理java byte类型工具类-Bytes类
- java socket server
- socket访问页面时返回了301重定向的代码,我该怎么解决?
- 引入thread后socket接受不了报文了
- Tomcat源码里,return (factory)有特殊作用么
- 新浪微博failed to receive access token
- 使用mybatis执行sql的时候为什么会出现Parameter index out of range (1 > number of parameters, which is 0)?
- 3年swing,1年JavaEE,1年java socket转android什么待遇
- Java socket通信
最新问答
更多- 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)