When exactly is BeginReceive on a socket considered completed?

Discussion in 'Microsoft C# .NET' started by Marcel Brekelmans, Aug 30, 2006.

  1. Hi,

    I use a socket to receive data from a certain process. I use the
    asynchronous operations BeginReceive() and EndReceive(), with a callback in
    BeginReceive. Now all documentation says that the callback, as specified in
    BeginReceive, is called as soon as 'the data is received'. But this is
    rather vague: when exactly is that moment if you don't know how exactly that
    other process is providing data?

    One criterium is that BeginReceive() is considered completed (and thus the
    callbask is called) when the buffer in the state object is filled upto the
    specified buffersize. But what if the 'delivering' process is feeding data
    in unknown quantities and in an irregular pattern? For instance, if it first
    delivers 100 bytes consecutively, and then there is a time interval of 1
    millisecond and another 200 bytes follow: does BeginReceive complete with
    100 bytes of incoming data? Or 300?

    Thanks for any insight.

    Marcel Brekelmans
     
    Marcel Brekelmans, Aug 30, 2006
    #1
    1. Advertisements

  2. In article <>, Marcel Brekelmans
    wrote:
    > But what if the 'delivering' process is feeding data
    > in unknown quantities and in an irregular pattern?


    Data arrives in packets that are split based on how much data the
    sending process supplies at a time but that may also be split while
    they're in transit. As far as the receiving application is concerned,
    the receive completes when a LAN packet is received. If there's more
    data, that is handled on the next receive.

    If you're using TCP, the protocol is designed to handle a logically
    continuous stream of octets/bytes without marked message start/finish.
    It's important to understand that packets may be arbitrarily split
    while in transit (or for that matter, by one of the end point hosts).
    Applications typically split data into messages by including either a
    message length or an end of message flag.

    Hope this helps.

    Mike
     
    Mike Blake-Knox, Sep 1, 2006
    #2
    1. Advertisements

  3. As Mike points out, you can't count on anything. You can only count on 1
    byte or 0 (i.e. closed). You need to "know" how many to expect and loop. I
    find the best way to do this is by prepending a length (or fixed length
    header that includes the data length) to the message, so then you know when
    the message is received. Appending a marker at the end can be problematic
    as you need to pick a marker that will never appear in the data stream.
    With a prepended Len, you don't need to worry about it.

    --
    William Stacey [MVP]

    "Marcel Brekelmans" <> wrote in message
    news:...
    | Hi,
    |
    | I use a socket to receive data from a certain process. I use the
    | asynchronous operations BeginReceive() and EndReceive(), with a callback
    in
    | BeginReceive. Now all documentation says that the callback, as specified
    in
    | BeginReceive, is called as soon as 'the data is received'. But this is
    | rather vague: when exactly is that moment if you don't know how exactly
    that
    | other process is providing data?
    |
    | One criterium is that BeginReceive() is considered completed (and thus the
    | callbask is called) when the buffer in the state object is filled upto the
    | specified buffersize. But what if the 'delivering' process is feeding data
    | in unknown quantities and in an irregular pattern? For instance, if it
    first
    | delivers 100 bytes consecutively, and then there is a time interval of 1
    | millisecond and another 200 bytes follow: does BeginReceive complete with
    | 100 bytes of incoming data? Or 300?
    |
    | Thanks for any insight.
    |
    | Marcel Brekelmans
    |
    |
     
    William Stacey [MVP], Sep 1, 2006
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dream machine

    Async Socket , BeginReceive question .

    dream machine, Jul 24, 2003, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    302
    dream machine
    Jul 29, 2003
  2. Guest

    Asynchronous Socket.BeginReceive() Problem

    Guest, Apr 23, 2004, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    425
    Guest
    Apr 23, 2004
  3. Guest

    Socket.BeginReceive() Problem

    Guest, Apr 23, 2004, in forum: Microsoft C# .NET
    Replies:
    0
    Views:
    246
    Guest
    Apr 23, 2004
  4. Mark Smith

    BeginReceive Async network socket example

    Mark Smith, Jun 13, 2004, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    805
    Jon Skeet [C# MVP]
    Jun 13, 2004
  5. Steve Richter

    confused by socket.BeginReceive

    Steve Richter, Jun 7, 2005, in forum: Microsoft C# .NET
    Replies:
    6
    Views:
    859
    Jon Skeet [C# MVP]
    Jun 8, 2005
Loading...

Share This Page