Async Socket , BeginReceive question .

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dream machine

    #1

    Async Socket , BeginReceive question .

    Hi all ,
    with BegeinReceive I can build async method of Socket Class
    that Receive the data from the Socket Client .

    My question is , if I have this code that create 3 Receive Async Call :

    <code>
    mySocket.BeginR eceive(param1,p aram2,param3... ..);
    mySocket.BeginR eceive(param1,p aram2,param3... ...);
    mySocket.BeginR eceive(param1,p aram2,param3... ...);

    myThread.wait()
    <code>

    and If the Client Socket send my 3 async Message, what will happened ?

    BeginReceive1, that receive message 1 ||| BeginReceive2, that receive message 2 ||| BeginReceive3, that receive message 3

    or :
    BeginReceive1, that receive Message 1
    BeginReceive2, that receive Message 2
    BeginReceive3, that receive Message 3

    The 3 SendMessage is listen at the same time from server ?

    or :

    The server listen the Message1 with BeginReceive1,
    server listen the Message 2 with BeginReceive2 when BeginReceive1 has finish to listen the Message 1
    server listen the Message 3 with BeginReceive3 when BeginReceive 2 has finish to listen the Message 2 .

    Thank all ,
    dm


















  • Rich Blum

    #2
    Re: Async Socket , BeginReceive question .

    "dream machine" <no@si.forse> wrote in message news:<O3y7ENbUD HA.3916@tk2msft ngp13.phx.gbl>. ..[color=blue]
    > Hi all ,
    > with BegeinReceive I can build async method of Socket Class
    > that Receive the data from the Socket Client .
    >
    > My question is , if I have this code that create 3 Receive Async Call :
    >
    > <code>
    > mySocket.BeginR eceive(param1,p aram2,param3... ..);
    > mySocket.BeginR eceive(param1,p aram2,param3... ...);
    > mySocket.BeginR eceive(param1,p aram2,param3... ...);
    >
    > myThread.wait()
    > <code>
    >
    > and If the Client Socket send my 3 async Message, what will happened ?
    >[/color]
    dm -

    This is somewhat of a trick question. Your last scenario is
    correct, the three BeginReceive() methods will "stack up", and process
    incoming data serially - one after the other. The first BeginReceive()
    method will process whatever data is available on the socket first,
    then the second BeginReceive() method will process any remaining data
    (or new data on the socket), and finally, the third one will process.

    The problem is that there is no control as to which BeginReceive()
    method will process what data. TCP is not a message-oriented protocol,
    it is stream-oriented. The three messages that are sent are not
    guaranteed to be taken out of the socket buffer in the same format by
    each of the three BeginReceive() methods that they were sent. Its
    quite possible that the first BeginReceive() method will grab all of
    message1, plus part of message2, then the second BeginReceive() method
    grabs the end of message2 plus all of message3, leaving the last
    BeginReceive() method never to complete, waiting for data. This would
    make a nightmare situation trying to put all of the data back
    together.

    It is better to either mark your messages with boundaries or send
    the message size before the actual message. That way the other end
    knows when each message starts and stops, and can process the received
    data properly. Hope this helps out some.

    Rich Blum - Author
    "C# Network Programming" (Sybex)

    "Network Performance Open Source Toolkit" (Wiley)

    Comment

    • dream machine

      #3
      Re: Async Socket , BeginReceive question .

      Hi rich ,
      thanks for your answer !

      dm


      Comment

      Working...