J
John J. Hughes II
Using the below code I am send multiple sterilized object across an IP port.
This works fine if only one object is received at a time but with packing
sometimes there is more then one object or half an object in the received
data. If I place the data in a memory stream on the received side is there
a way to determine where one ends and the next one start?
Since the deserializer stream seems to move the pointer I am trying to look
at the next couple bytes to determine if there is more data in the stream.
It seems that serialized data starts with 0,1 but I can not confirm this.
Is there maybe a rule?
I have also noticed that the number of bytes recieved is always bigger then
the number of bytes deserizlized, any clue why?
CODE 1
IFormatter f = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
f.Serialize(ms, cmd);
byte[] b = ms.GetBuffer();
this.socket.BeginSend(b, 0, b.Length, SocketFlags.None, new
AsyncCallback(Send_Callback), this.socket);
CODE 2
IFormatter i = new BinaryFormatter();
MemoryStream ms = new MemoryStream(o, 0, len);
ms.Position = 0;
while(ms.Position < len)
{
long position = ms.Position;
if(!(ms.ReadByte() == 0 && ms.ReadByte() == 1))
break;
ms.Position = position;
object obj = i.Deserialize(ms);
.....
Regards,
John
This works fine if only one object is received at a time but with packing
sometimes there is more then one object or half an object in the received
data. If I place the data in a memory stream on the received side is there
a way to determine where one ends and the next one start?
Since the deserializer stream seems to move the pointer I am trying to look
at the next couple bytes to determine if there is more data in the stream.
It seems that serialized data starts with 0,1 but I can not confirm this.
Is there maybe a rule?
I have also noticed that the number of bytes recieved is always bigger then
the number of bytes deserizlized, any clue why?
CODE 1
IFormatter f = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
f.Serialize(ms, cmd);
byte[] b = ms.GetBuffer();
this.socket.BeginSend(b, 0, b.Length, SocketFlags.None, new
AsyncCallback(Send_Callback), this.socket);
CODE 2
IFormatter i = new BinaryFormatter();
MemoryStream ms = new MemoryStream(o, 0, len);
ms.Position = 0;
while(ms.Position < len)
{
long position = ms.Position;
if(!(ms.ReadByte() == 0 && ms.ReadByte() == 1))
break;
ms.Position = position;
object obj = i.Deserialize(ms);
.....
Regards,
John