how many async socket requests can be going on on the same socket?

前端 未结 2 966
有刺的猬
有刺的猬 2021-02-06 01:46

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 anot

2条回答
  •  北海茫月
    2021-02-06 02:27

    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.

提交回复
热议问题