How to ensure that a Serialized object is completely read over a Socket ?

Posted by Amitd on Stack Overflow See other posts from Stack Overflow or by Amitd
Published on 2010-05-27T06:16:16Z Indexed on 2010/05/27 6:21 UTC
Read the original article Hit count: 269

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets