How to ensure that a Serialized object is completely read over a Socket ?
- by Amitd
Hi guys,
I am trying to write a socket server and Client that communicate with each other via serialized Objects.eg:To Login, Client sends server a serialized Login object and then the
server deserialises the object to read login details.
Similarly for other types of request/response.
I just wanted to be sure if Socket.receive() can read(untill) large serialized objects completely.
I have tried this code but seems to fail when a large object is serialised and sent over the internet.(seems to work fine in LAN situations.)
http://stackoverflow.com/questions/2134356/sending-large-serialized-objects-over-sockets-is-failing-only-when-trying-to-grow
using(MemoryStream ms = new MemoryStream()) {
int bytesRead;
while((bytesRead = m_socClient.Receive(buffer)) > 0) {
ms.Write(buffer, 0, bytesRead);
}
// access ms.ToArray() or ms.GetBuffer() as desired, or
// set Position to 0 and read
}
Are there any other ways to ensure that the object gets completely read.