views:

262

answers:

2

When I call BeginSend on a socket, I pass a delegate which will be called (by a different thread) when the data has been sent.

What would happen if I call BeginSend another time while the first has not yet been 'callbacked'?

What is the correct behavior to send data? do BeginSend, and on the callback do EndSend and start another send? Or is it actually wise to have multiple BeginSends working at the same time?

this is the BeginSend page on MSDN which doesn't give an answer to this question: BeginSend msdn

+2  A: 

Based I what I read here, it seems like having multiple concurrent BeginSend is do-able.

Excerpt:

  1. You can queue multiple BeginSends at the same time. YOu don't need to lock
  2. If you don't create a callback method, how do you know when the send is successful? You may want to ignore the success - more like a fire and forget - method, but then you at least need to know when it is safe to end your program.
  3. The other thing you can do is use the IAsyncResult that you get from BeginSend. You can use the WaitHandle to wait for the operation to complete. But that defeats the whole purpose of using Async in the first place.
  4. My recommendation is to use a callback, and make sure that the callback is called and the number of bytes you sent is actually sent and gracefuly end the send operation.

Update:
Tried simultaneous BeginSend based on the codes on MSDN and data are sent without exception. However do keep in mind that the connection for the same socket must be opened prior. Simultaneous BeginConnect will not work.

o.k.w
So this multiplexes multiple independent data streams over a single socket? Is this Microsoft's answer to SCTP?
Robert S. Barnes
interesting. But is it faster than just sending packet per packet with only one pending BeginSend? I'm asking this since I'm making a server which should have a good performance, and it might be that one should have at least X pending BeginSends to have decent throughput. At this moment, I wouldn't know
Toad
Robert - No, not at all. If the socket is a TCP socket then you have a single stream. Issuing multiple `BeginSend` calls simply allows you queue multiple writes to the stream asynchronously.
Len Holgate
+3  A: 

As O.K.W. says, multiple pending BeginSend calls will work fine. You probably DO need to bear in mind some things though.

Firstly if this is a TCP socket then this is still a single stream of data from peer to peer.

Secondly if all your BeginSend calls occur on the same thread then the result will be that the peer receives the data in the order of the calls. If your BeginSend calls occur from different threads then the data could arrive in any order as there will likely be a race condition between each of the sends. This may or may not matter to you (depends if you're sending discrete, complete messages with each send or not).

Thirdly if you are using TCP and sending faster than the code at the other end of the socket can receive then you may fill the TCP Window and the TCP stacks will start to perform flow control on your data stream. If you continue to issue BeginSend calls then you MAY end up in a situation where your callbacks take longer and longer to be called as the TCP stack on your server queues data to send (you only get the callback once the data has been sent and the TCP Window based flow control will be preventing new data being sent until the TCP Window is no longer 'full'; i.e. the peer has sent ACKs for some of the data that is in flight).

You can then get into a situation whereby you are using up resources on the sending machine in an uncontrollable manner (you issue a BeginSend and have no idea when it will complete and each send uses memory for the buffer being sent and potentially non-paged pool down in the Winsock code... Non-paged pool is a system wide resource and is quite a scarce on pre Vista OSs and some badly behaved drivers can blue screen the box if non-paged pool is low or exhausted. What's more you may also be locking pages of memory into memory and there's another system wide limit on the number of locked memory pages.

Because of these issues it's usually best to implement your own protocol level flow control which limits the number of BeginSend calls that can be pending at any one time (using a protocol level ACK, perhaps) or to work with the TCP Window flow control and use the completion of a pending send to issue a new send and you can queue data to send in your own memory and have complete control over the resources used and what you do if you queue "too much" data.

See this reply: http://stackoverflow.com/questions/1997691/what-happens-when-tcp-udp-server-is-publishing-faster-than-client-is-consuming/1998127#1998127 for more information on TCP Window flow control and what happens with overlapped I/O (in C++ land) when you ignore it and issue too many overlapped sends...

In summary, posting multiple concurrent BeginSend calls is the way to optimum TCP data flow but you need to make sure you don't send "too fast" as once you do you are consuming resources in a manner which you can't control and which is potentially fatal for the machine on which your code is running. So don't allow an unbounded number of BeginSend calls to be outstanding and, ideally, profile the box to ensure that you are not exhausting system wide resources.

Len Holgate
@Lens, that's a good one +1
o.k.w
@lens: I have windowing built into the protocol I'm sending (smpp) so their would never be a matter of sending too fast. The out of order point you bring up would be an issue though.
Toad
@Len, misspelled your name in my comment above, my apology :)
o.k.w