使用套接字发送数组 C# winforms

send array with socket C# winforms

如何使用java 'outputstream' 之类的套接字发送数组? 你能看到任何演示或示例序列化吗? 我可以用套接字向我的客户发送一个简单的文本。但是我怎样才能将数组、列表或 Class 发送给我的客户。我想发送这种格式 列表数组;

Java的outputstream也不能做你想做的,它所能做的就是发送byte[],这正是C#的套接字类所做的。

如果你想发送复杂的对象,你必须使用某种形式的 "Serializer",这样你就可以将你的对象转换为 byte[] 以发送出去。

.NET 中内置的一个易于使用的序列化程序是 XmlSeralizer, this will produce a string that you can then feed in to a StreamWriter,它将字符串转换为 byte[] 并将其写到套接字上。另一端只会使用 StreamReader.

的反向过程

如果您不想使用那个中间文本步骤,我 NOT 建议使用 BinaryFormatter like you see frequently on the internet, it is very "fragil" and having different levels of .NET Windows updates installed on both ends can end up breaking it. Instead I recommend using a 3rd party binary serializer like Protobuf-net