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

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

Peter Duniho

jkv said:
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? [...]

I don't recall the specifics of the serialization, but it must be
providing some kind of byte count or delimiting in the stream for it to
work.

One method of delimiting would be to send an XML document. XML
documents have a specific construction, and for a well-formed one, it's
easy to tell when it ends, because you reach the last closing tag for
the document.

Other data structures are similarly self-delimiting, or one can use some
specific type of delimiter (which could in turn require adding quoting
behavior, so that the delimiter can appear within the data without
interfering with the actual delimiting of the data).

Alternatively, the serialization code could just preface the data with a
count of bytes that is read at the other end.

The bottom line here is that AFAIK serialization has the design
requirement that you can serialize multiple objects to a stream, and
successfully deserialize those objects from the stream.

There's nothing special in TCP that would enable this behavior; all that
TCP (and thus TcpClient) could provide is the stream itself. But I
believe that the .NET serialization does in fact include some type of
delimiting or byte counting (I'd guess delimiting, but I don't know for
sure) so that no matter what the stream you're using is, it works as
desired.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top