二进制格式化程序异常

发布于 2024-10-31 06:47:03 字数 533 浏览 3 评论 0原文

我正在尝试将对象图从服务器进程移动到客户端。它有效。至少当客户端和服务器都在我的开发虚拟机上时它可以工作。当我在我的基础机器上运行服务器(开发虚拟机上的客户端)时,它也可以工作。

但当我在媒体中心电脑上运行服务器时,它停止工作。例外的是:

二进制流“0”不包含有效的 BinaryHeader。可能的原因是序列化和反序列化之间无效的流或对象版本更改。

所有三台 PC 均为 x64 Windows 7 计算机。我使用 TCPClient 和 TCPListener 以及 BinaryFormatter 类来完成繁重的工作。

使用标准 FileStream 对象从文件中读取正在传输的数据。

如果在客户端我将缓冲区序列化到文件中,则内容(根据 BeyondCompare)实际上似乎有所不同?!?

我的对象上的所有字符串属性均在 setter 中进行 Base64 编码,并在 getter 中进行解码。

我可以发布代码,但我不确定问题所在?有什么想法吗?

I'm attempting to move an object graph from a server process to a client. And it works. At least it works when the both the client and server are on my dev virtual machine. It also works when I run the server on my base machine (client on dev vm).

It stops working though, when I run the server on my media centre PC. The exception is:

Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.

All three PC's are x64 Windows 7 machines. I'm using TCPClient and TCPListener along with the BinaryFormatter class to do the heavy lifting.

The data being transferred is read from a file using a standard FileStream object.

If at the client end I serialize the buffers to a file, the contents (according to BeyondCompare) do actually seem to differ?!?

All string properties on my objects are Base64 encoded in the setters and decoded in the getters.

I can post code but I'm not sure where the problem area lies? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-11-07 06:47:03

更新:我似乎已经解决了这个问题。我有一个断点,客户端读取服务器响应

tcpClient.GetStream().Read(buffer, 0, buffer.Length);

并注意到从“问题”服务器读取的字节较少。经过快速谷歌后,我发现这篇文章 http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/759f3f2f-347b-4bd8-aa05-fb7f681c3426 其中 Dave Murray 建议:

有几种方法可以处理这个问题
更优雅。如果你不是
计划重用该连接
还有什么,最简单的是什么
nobugz 建议。当服务器是
发送数据完毕,关闭()
连接结束了。当
客户端已读取之前发送的所有数据
关闭,读取将开始返回
0,此时你就知道服务器了
不打算发送任何东西
否则。

因此,我将我的代码从一次读取更新为:

var buffer = new byte[32768];
var totalBytesRead = 0;
var bytesRead = tcpClient.GetStream().Read(buffer, 0, buffer.Length);

do
{
      totalBytesRead += bytesRead;
      bytesRead = tcpClient.GetStream().Read(buffer, totalBytesRead, bytesRead);

} while (bytesRead > 0);

并更新了我的服务器代码以按照帖子关闭连接。根据 @leppie 的评论,我可能可以删除我的 Base64 包装属性......

Update: I've seemingly solved this issue. I had a breakpoint where the client read the server response

tcpClient.GetStream().Read(buffer, 0, buffer.Length);

and noted that fewer bytes were read from the "problem" server. After a quick google I found this article http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/759f3f2f-347b-4bd8-aa05-fb7f681c3426 in which Dave Murray suggests:

There are a couple ways to handle this
more elegantly. If you're not
planning on reusing the connection for
anything else, the easiest is what
nobugz suggests. When the server is
done sending data, have it Close()
it's end of the connection. When the
client has read all data sent before
the Close, Read will start returning
0, at which point you know the server
isn't planning on sending anything
else.

So i updated my code from a single read to:

var buffer = new byte[32768];
var totalBytesRead = 0;
var bytesRead = tcpClient.GetStream().Read(buffer, 0, buffer.Length);

do
{
      totalBytesRead += bytesRead;
      bytesRead = tcpClient.GetStream().Read(buffer, totalBytesRead, bytesRead);

} while (bytesRead > 0);

and updated my server code to close the connection as per the post. And according to the comment from @leppie i can probably remove my Base64 wrapped properties...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文