J
jkv
Hi list,
The code in the end of this mail is a snippet from a application im
working on.
A tcp client serializes 10 FOO objects and sends them to the server
which deserializes them - all is working fine.
Thou, im wondering how the receiver (server snippet) tells the 10
objects apart - im just sending 10 byte arrays without telling the
size... Any hints?
Im wondering if my code is working by pure "luck", or maybe the
correct way of doing this is to make the client notify the server
about the length of the object which is to be transmitted?
Regards,
Johnny
---CLIENT SNIPPET---
NetworkStream stream = client.GetStream();
//sendlist is a list<FOO> which contains 10 FOO objects
foreach (FOO obj in sendlist)
{
MemoryStream ms = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
stream.Write(ms.ToArray(), 0, ms.ToArray().Length);
}
---SERVER SNIPPET---
stream = client.GetStream();
List<FOO> receivedobj = new List<FOO>();
IFormatter formatter = new BinaryFormatter();
//_NumberOfObjectToReceive is int 10 (sent from client earlier in the
code)
for (int i = 0; i < _NumberOfObjectToReceive; i++)
{
FOO obj = (FOO)formatter.Deserialize(stream);
receivedobj.Add(obj);
}
The code in the end of this mail is a snippet from a application im
working on.
A tcp client serializes 10 FOO objects and sends them to the server
which deserializes them - all is working fine.
Thou, im wondering how the receiver (server snippet) tells the 10
objects apart - im just sending 10 byte arrays without telling the
size... Any hints?
Im wondering if my code is working by pure "luck", or maybe the
correct way of doing this is to make the client notify the server
about the length of the object which is to be transmitted?
Regards,
Johnny
---CLIENT SNIPPET---
NetworkStream stream = client.GetStream();
//sendlist is a list<FOO> which contains 10 FOO objects
foreach (FOO obj in sendlist)
{
MemoryStream ms = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
stream.Write(ms.ToArray(), 0, ms.ToArray().Length);
}
---SERVER SNIPPET---
stream = client.GetStream();
List<FOO> receivedobj = new List<FOO>();
IFormatter formatter = new BinaryFormatter();
//_NumberOfObjectToReceive is int 10 (sent from client earlier in the
code)
for (int i = 0; i < _NumberOfObjectToReceive; i++)
{
FOO obj = (FOO)formatter.Deserialize(stream);
receivedobj.Add(obj);
}