2

我有一个ObservableCollection<MyClass>,我序列化它的内容并使用套接字发送它。按照代码:

private const int Port = 3762;
private static SocketPermission permission = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "", Port);
private static IPHostEntry ipEntry = Dns.GetHostEntry(string.Empty);
private static IPAddress ipAddress = ipEntry.AddressList[0];
private static IPEndPoint ipPoint = new IPEndPoint(ipAddress, Port);

Socket sTransmissao = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

using (var stream = new MemoryStream())
{
    var binary = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    binary.Serialize(stream, TransmissaoPendencias);
    byte[] bArray = new byte[stream.Length];
    stream.Seek(0, SeekOrigin.Begin);
    stream.Read(bArray, 0, (int)stream.Length);

    sTransmissao.Connect(ipAddress, Port);
    sTransmissao.Send(bArray);
}

但是字节长度会随着我收藏中的项目数量而变化。

在客户端检索此信息的可行解决方案是什么?

提前致谢。

4

1 回答 1

3

您需要在发送数组之前传输大小,以便另一端知道要读取多少。

于 2013-06-03T13:34:10.420 回答