3

If I call the c# asynchronous method continuously as shown below:

socket.BeginSend(data1, 0, data1.Length, 0,
        new AsyncCallback(SendCallback1), handler);
socket.BeginSend(data2, 0, data2.Length, 0,
        new AsyncCallback(SendCallback2), handler);

Can asynchronous method ensure the order of data?

Are other network Libraries that support asynchronous operation considered the problem?
how are they implemented to guarantee the data order in asynchronous operation internally?

atomd
  • 558
  • 1
  • 5
  • 11

2 Answers2

2

No I don't think that asynchronous method would guarantee that the data you are sending will be sent in order or calling or sending.

As Marc said even if it does work note this is not the right approach, you should not call two BeginSend side by side or one after another. What you can do is call EndSend in the call back method of first BeginSend and then inside that callback call the next BeginSend.

This is a nice MSDN article on the topic read it first before moving forward

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
1

Normally you would only do the second BeginSend inside the callback from the first, after calling EndSend. I would actually hope the library would throw an exception if there was already an incomplete BeginSend in progress. But if it doesn't throw - I doubt order would be guaranteed (I doubt success would be guaranteed either).

You might also consider the async CTP which has continuations for this sort of thing, allowing you to "await" a send and then perform the additional work as a continuation.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900