Cancel blocking AcceptTcpClient call

后端 未结 5 1495
生来不讨喜
生来不讨喜 2020-12-09 18:00

As everyone may already know, the simplest way to accept incoming TCP connections in C# is by looping over TcpListener.AcceptTcpClient(). Additionally this way will block co

5条回答
  •  无人及你
    2020-12-09 18:39

    Cancelling AcceptTcpClient

    Your best bet for cancelling the blocking AcceptTcpClient operation is to call TcpListener.Stop which will throw a SocketException that you can catch if you want to explicitly check that the operation was cancelled.

           TcpListener serverSocket = new TcpListener ( serverAddr, serverPort );
    
           ...
    
           try
           {
               TcpClient serverClient = serverSocket.AcceptTcpClient ();
               // do something
           }
           catch (SocketException e)
           {
               if ((e.SocketErrorCode == SocketError.Interrupted))
               // a blocking listen has been cancelled
           }
    
           ...
    
           // somewhere else your code will stop the blocking listen:
           serverSocket.Stop();
    

    Whatever wants to call Stop on your TcpListener will need some level of access to it, so you would either scope it outside of your Listen method or wrap your listener logic inside of an object that manages the TcpListener and exposes Start and Stop methods (with Stop calling TcpListener.Stop()).

    Async Termination

    Because the accepted answer uses Thread.Abort() to terminate the thread it might be helpful to note here that the best way to terminate an asynchronous operation is by cooperative cancellation rather than a hard abort.

    In a cooperative model, the target operation can monitor a cancellation indicator which is signaled by the terminator. This allows the target to detect a cancellation request, clean up as needed, and then at an appropriate time communicate status of the termination back to the terminator. Without an approach like this, abrupt termination of the operation can leave the thread's resources and possibly even the hosting process or app domain in a corrupt state.

    From .NET 4.0 onward, the best way to implement this pattern is with a CancellationToken. When working with threads the token can be passed in as a parameter to the method executing on the thread. With Tasks, support for CancellationTokens is built into several of the Task constructors. Cancellation tokes are discussed in more detail in this MSDN article.

提交回复
热议问题