How does tcpclient.getstream know when a serialized objects ends?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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);
}
.



Relevant Pages

  • Re: About kontract pre and post condition
    ... either in 'foo' or in the client. ... behavior responsibility and that is none of the client's business. ...
    (comp.object)
  • Re: LSP and subtype
    ... I'm I right if I say that if the client can use this foo from Sub insted of foo from Base for example by using polymorfism and the client doesn't see any difference then Sub is a subtype of Base. ... Not only do we have to perform more surgery on the tree, we have to change the calling context in Client to invoke SubA.foorather than Base.foo. ...
    (comp.object)
  • Re: Synchronized objects
    ... > proxies to remote objects and the appropriate one to use depends on how ... > plan to use the object in your clients and on your server. ... > 1) Does every client that connects need to access the same Foo instance ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: A roguelike programming contest
    ... a client to directly include headers needed only indirectly ... Bar _bar; ... Your proscription would force a module using Foo to write: ... Forward declarations /where possible/ help reduce compile ...
    (rec.games.roguelike.development)
  • Re: slow file open
    ... I can't tell from this small snippet and no context. ... neither does it matter whether the client is on or off. ...
    (microsoft.public.windows.server.general)