none
Socket.Receive() multiple calls necessary RRS feed

  • Question

  • Hi,


    When larger byte[]s are Sent/Received, we need to call Received multiple time, to get the sent amount of data.

    Reading the MS docu (http://msdn.microsoft.com/de-de/library/26f591ax.aspx) it states:

    'If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the number of bytes specified by the size parameter.'

    For me this means, that we get all the sent data in *one* call, not in several.

    Did we miss something? Or is there a link to how to officially use it. Example, etc?

    Thanks,

    Christian

    Friday, July 19, 2013 3:16 PM

Answers

  • "For me this means, that we get all the sent data in *one* call, not in several."

    It's certainly doesn't mean that, it say nothing about "sent" data. Receive returns whatever data is already available in the client buffers, it doesn't wait for more data to arrive nor does it know how much data will arrive.

    Normally you have to keep calling Receive in a loop until all the expected data is received. Never assume that a Send call is matched exactly by one Receive call.

    Saturday, July 20, 2013 5:37 AM
    Moderator

All replies

  • "For me this means, that we get all the sent data in *one* call, not in several."

    It's certainly doesn't mean that, it say nothing about "sent" data. Receive returns whatever data is already available in the client buffers, it doesn't wait for more data to arrive nor does it know how much data will arrive.

    Normally you have to keep calling Receive in a loop until all the expected data is received. Never assume that a Send call is matched exactly by one Receive call.

    Saturday, July 20, 2013 5:37 AM
    Moderator
  • Thanks for your answer!

    I assume same applies for Socket.ReceiveAsync(...)? Where would I find that in MSDN documentation? http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receiveasync.aspx doesn't state anything about that.

    Thanks,

    Christian

    Monday, July 22, 2013 8:49 AM
  • "doesn't state anything about that."

    I believe it does:

    "For byte stream-style sockets, incoming data is placed into the buffer until the buffer is filled, the connection is closed, or the internally buffered data is exhausted."

    In general, for TCP sockets there's no way that any kind of "receive" method can be paired with a "send". It's a stream of bytes, not a stream of some packets/messages/etc.

    Monday, July 22, 2013 11:01 AM
    Moderator
  • Thanks for clarification.

    Just curious:

    we send/receive data as following:

    1) a 4 bytes header, containing a int byte[] representation, indicating the length of payload byte[]

    2) payload

    So we can not even assume the 4 bytes header are received by one receive?

    Monday, July 22, 2013 12:17 PM
  • "So we can not even assume the 4 bytes header are received by one receive?"

    Nope. There's a good change to get 4 bytes but there's no guarantee, you may also get a single byte.

    Monday, July 22, 2013 1:11 PM
    Moderator
  • Thanks for all the answers!
    Wednesday, July 24, 2013 6:55 AM