C# 通过异步套接字连接发送带有序列化对象的对象大小

发布于 2024-10-01 05:14:26 字数 275 浏览 8 评论 0原文

我想序列化一个对象并通过网络发送它。我已经使用我的类上的 ISerializeable 属性和 BinaryFormatter 将其设置为将对象转换为字节。我可以发送对象并在接收端反序列化它。但是,为了确保在尝试重建对象之前拥有整个对象,我想将大小与流一起发送。我想将前几个字节设置为大小,检查接收到的数据何时至少达到此固定大小,然后我可以读取该数据并获取对象的完整大小。然后,只需等待我收到的数据等于对象的大小+固定大小的字节即可。如何在流中偏移数据,以便可以发送 int 将对象的大小存储为前几个字节,并将对象的大小存储为剩余字节?

I want to serialize an object and send it over the network. I have set it up using ISerializeable attribute on my class and BinaryFormatter to convert the object to bytes. I can send the object and deserialize it on the receiving end. However, in order to assure that I have the entire object before trying to reconstruct it, I want to send the size along with the stream. I'd like to set the first few bytes as the size, check when the received data is at least this fixed size, then I can read that and get the full size of the object. Then, it's simply a matter of waiting until my received data is the size of the object+ the fixed size bytes. How can I offset my data in my stream so that I can send an int to store the size of the object as the first few bytes, and my object as the remaining bytes?

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

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

发布评论

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

评论(4

淡淡绿茶香 2024-10-08 05:14:26

正确的答案是:

SocketPacket socketData = (SocketPacket)asyn.AsyncState;
socketData.currentSocket.EndReceive(asyn);
byte[] data = socketData.dataBuffer;   

从 SocketPacket 读取数据不是的正确方法吗

The right answer is:

SocketPacket socketData = (SocketPacket)asyn.AsyncState;
socketData.currentSocket.EndReceive(asyn);
byte[] data = socketData.dataBuffer;   

Is not the right way to read from a SocketPacket

氛圍 2024-10-08 05:14:26
MemoryStream resultStream = new MemoryStream();
resultStream.Write(BitConverter.GetBytes(objectStream.Length));
resultStream.Write(objectStream.ToArray());

// send the resultStream
MemoryStream resultStream = new MemoryStream();
resultStream.Write(BitConverter.GetBytes(objectStream.Length));
resultStream.Write(objectStream.ToArray());

// send the resultStream
那支青花 2024-10-08 05:14:26

您可以序列化为 MemoryStream,然后在完成后添加 MemoryStream 中的 .Length,然后添加数据(使用 >GetBuffer() 并从数组中复制 .Length 字节)。在接收器处倒车;读取长度(通常为 4 个字节),然后将这么多数据打包到 MemoeryStream 中;倒带 MemoryStream (Position=0) 并反序列化。当然,你需要同意字节序等。

(看,我什至没有提到prot......哦,另一件事)

Can you just serialize to a MemoryStream, then once that is done add the .Length from the MemoryStream, followed by the data (use GetBuffer() and copy .Length bytes from the array). Reverse at the receiver; read the length (typically 4 bytes), then pack that much data into a MemoeryStream; rewind the MemoryStream (Position=0) and deserialize. Of course, you need to agree endianness etc.

(see, and I didn't even mention prot... oh, that other thing)

有木有妳兜一样 2024-10-08 05:14:26

你究竟想阻止什么? TCP 已经具有内置的一致性和可靠性检查。

What exactly are you trying to prevent? TCP already has built-in consistency and reliability checks.

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