我有一个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);
}
但是字节长度会随着我的集合中的项目数量而变化。
在客户端检索此信息的可行解决方案是什么?
提前致谢。
答案 0 :(得分:3)
您需要在发送数组之前传输大小,以便另一端知道要读取多少内容。