sockets - c# stream received all data? -


I'm using the System.Net.Sockets namespace from the C # NET and Socket class. I am using asynchronous receiving methods. I think it can be done easily with something like a web service; This question is out of my curiosity, rather than the practical requirement.

My question is this: Suppose the client is sending some binary-serialized objects of unknown length. On my server with socket, how do I know that the whole object has been received and it is ready for deserialization? I consider the object in the byte as a priority with the length of the object, but it seems unnecessary in the net world. What happens if buffer is larger than buffer? How do I know that 'oh buffer size change because the object is too big'?

You need the protocol to terminate either ( Such as XML, effectively - you know that when you get an XML document, when it closes the original element) or you or the length-prefix When it is done, you need the other end to close the stream.

In the case of self-terminal protocol, you have enough hook so that the reading code can say that when it's finished. You can not have enough hooks with binary serialization. The length prefix is ​​by far the easiest way.


Comments