简体   繁体   English

boost asio:0字节写入

[英]boost asio: 0 byte write

I need some help understanding a peculiar issue I'm having when using asio. 我需要一些帮助来了解使用asio时遇到的特殊问题。

I've a client -server app, with a c++ client(using boost asio) sending 2 byte hearbeat (say, every second) to a server(written in java) (and receiving lots of data as well). 我有一个客户端服务器应用程序,一个c ++客户端(使用boost asio)向服务器(用Java编写)发送2字节的心跳信号(例如,每秒)(并接收大量数据)。

for a quite a few minutes the server correctly receives the 2 byte HeartBeat, but after that the server's 'read' complains abt a 0 byte read, and closes the connection (which I guess is correct for a blocking read). 在相当长的几分钟内,服务器正确接收了2字节的HeartBeat,但是在那之后服务器的“读取”抱怨0字节的读取,并关闭了连接(我想这对于阻塞读取是正确的)。 The client however always prints out that it's been transferring the correct amount. 但是,客户端总是打印出它正在转移正确的金额。

I've experimented with almost all variants of the 'write' family of functions. 我已经尝试了“写”函数系列的几乎所有变体。 are all of them implemented in terms of 'write_some' and does that mean that this behavior is expected? 它们是否全部以'write_some'的方式实现,这是否意味着这种行为是预期的?

I must be making some mistake in my usage, basically I'm looking for something within asio that guarantees a write ( at least a byte) . 我在使用时一定会犯一些错误,基本上我是在asio中寻找可以保证写入(至少一个字节)的内容。 please help me figure out where I'm going wrong(and if any further info is reqd.)... any advice, most appreciated! 请帮助我找出问题出在哪里(如果需要进一步的信息,则...)...任何建议,非常感谢! thanks! 谢谢!

If it's sockets, you can't "guarantee a write"; 如果是套接字,则不能“保证写入”;不能使用套接字。 what if the network is down, the cable yanked out, the switch is on fire, or the power is out worldwide and your computer happens to be the only one running on batteries? 如果网络出现故障,电缆被拔出,交换机着火或世界范围内的电源断开并且您的计算机碰巧是唯一用电池运行的计算机,该怎么办?

That said, it sounds as if you have some kind of buffering/emptying issue perhaps, check over your read code to make sure it really consumes all data that appears. 就是说,听起来好像您有某种缓冲/清空问题,请检查读取的代码以确保它确实消耗了所有出现的数据。

A 0-byte read is not an error, look over that code again, check for any error status flags on the socket(s) and so on. 读取0字节不是错误,请再次查看该代码,检查套接字上的任何错误状态标志,依此类推。 A read can fail with a "AGAIN"-status, which really means you should try again. 读取可能会失败并显示“ AGAIN”状态,这实际上意味着您应该重试。

strace the applications at both ends. 在两端strace应用程序。 It will show any error codes that are returned by read (), write () etc. Use strace -f if the application is multithreaded. 它将显示由read (), write ()等返回的所有错误代码。如果应用程序是多线程的,则使用strace -f

The advantage of this approach is that all applications - java, c++, python appear the same in an strace , so it's easy to spot bad behaviour. 这种方法的优势在于,所有应用程序-java,c ++,python在strace中看起来都相同,因此很容易发现不良行为。

In this case, it would probably show that the tcp connection ended (gracefully). 在这种情况下,它可能表明tcp连接已结束(正常)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM