none
Any way to cancel "Socket.ReceiveAsync()" ? RRS feed

  • Question

  • If my process has called SocketAsyncEventArgs.Socket.ReceiveAsync() then now the socket is in use and so, if I don't receive any more data, I can't perform another operation (e.g. SetBuffer() or SendAsync()) until it receives some data and I handle the Completed event.

    (If I try, I get "An asynchronous socket operation is already in progress using this SocketAsyncEventArgs instance." exception)

    Is there a way of reclaiming this socket (i.e. take it out of "listening" mode) to make it available for other operations again, but without closing the socket?

    Failing that, is it possible to somehow "fake" data being sent to the socket so that it will be released?



    • Edited by Colmde Monday, June 12, 2017 2:47 PM
    • Moved by CoolDadTxModerator Monday, June 12, 2017 5:42 PM Networking related
    Monday, June 12, 2017 2:10 PM

All replies

  • Hi Colmde,

    >> Any way to cancel "Socket.ReceiveAsync()" ?

    I am afraid we could not cancel the asynchronous receive.

    >> is it possible to somehow "fake" data being sent to the socket so that it will be released?

    What operation will you do before completing ReceiveAsync? What will you fo after releasing socket?

    I would suggest you check below link to see whether the work around works for you.

    # Cancelling a Socket.xxxxAsync call

    https://stackoverflow.com/questions/563593/cancelling-a-socket-xxxxasync-call

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 13, 2017 2:52 AM
  • Unfortunately I need to be able to do it without closing the socket - if I close the socket, the client gets disconnected, but what I want to do is perform a "SendAsync" to the same client.

    I have read elsewhere ( www.pcreview.co.uk/threads/c-async-socket-timeouts.3524297/ - sorry I can't post links )  that I could do a P/Invoke to CancelIoEx(), but I can't seem to find much documentation online about how to do this. Is this something that could do what I require, and if so, are there any examples available of doing it from within .NET? I tried 

    [DllImport("kernel32.dll", CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CancelIoEx(IntPtr hFile);

    and then

    bool result = CancelIoEx(saeaCommunicationObject.AcceptSocket.Handle)

    before the SendAsync, but I get result = false and still get the same exception afterwards when performing the SendAsync. (I also tried CancelIo() and got result = true, but still got the exception on SendAsync)

    If, on the other hand, I could somehow send a small piece of data to the socket from the server, my guess is that this would end the "ReceiveAsync" operation and I could have the "Completed" event ignore it and get on with my SendAsync.

    • Edited by Colmde Tuesday, June 13, 2017 8:19 AM
    Tuesday, June 13, 2017 7:45 AM
  • Hi Colmde,

    Could you share us a simple demo which could reproduce your issue through OneDrive? And then we could try to reproduce your issue at our side.

    In addition, I suggest you check whether below link is helpful to you.

    # How to cancel socket operations

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/a24774e9-da00-4708-9493-81f85964711d/how-to-cancel-socket-operations?forum=wsk

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 14, 2017 2:52 AM