Stopping a TcpListener after calling BeginAcceptTcpClient

前端 未结 5 532
花落未央
花落未央 2021-01-31 18:38

I have this code...

internal static void Start()
{
    TcpListener listenerSocket = new TcpListener(IPAddress.Any, 32599);
    listenerSocket.Start();
    listen         


        
5条回答
  •  旧巷少年郎
    2021-01-31 19:33

    try this one. it works fine for me without catching exceptions.

    private void OnAccept(IAsyncResult pAsyncResult)
    {
        TcpListener listener = (TcpListener) pAsyncResult.AsyncState;
        if(listener.Server == null)
        {
            //stop method was called
            return;
        }
        ...
    }
    

提交回复
热议问题