Async Socket , BeginReceive question .

D

dream machine

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.BeginReceive(param1,param2,param3.....);
mySocket.BeginReceive(param1,param2,param3......);
mySocket.BeginReceive(param1,param2,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
 
R

Rich Blum

dream machine said:
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.BeginReceive(param1,param2,param3.....);
mySocket.BeginReceive(param1,param2,param3......);
mySocket.BeginReceive(param1,param2,param3......);

myThread.wait()
<code>

and If the Client Socket send my 3 async Message, what will happened ?
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)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top