TcpListener: how to stop listening while awaiting AcceptTcpClientAsync()?

前端 未结 7 814
醉话见心
醉话见心 2020-12-29 07:14

I don\'t know how to properly close a TcpListener while an async method await for incoming connections. I found this code on SO, here the code :

public class         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 07:35

    Calling StopListening (which disposes the socket) is correct. Just swallow that particular error. You cannot avoid this since you somehow need to stop the pending call anyway. If not you leak the socket and the pending async IO and the port stays in use.

提交回复
热议问题