jjzjj

AcceptTcpClientAsync

全部标签

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

我不知道如何在异步方法等待传入连接时正确关闭TcpListener。我在SO上找到了这段代码,这里是代码:publicclassServer{privateTcpListener_Server;privatebool_Active;publicServer(){_Server=newTcpListener(IPAddress.Any,5555);}publicasyncvoidStartListening(){_Active=true;_Server.Start();awaitAcceptConnections();}publicvoidStopListening(){_Active=

c# - AcceptTcpClient 和 AcceptTcpClientAsync 区别

据我了解,这些行:varclient=awaitlistener.AcceptTcpClientAsync();varclient=listener.AcceptTcpClient();应该以同样的方式工作,但是如果我尝试打开,例如,5k连接(而不关闭它们),监听器(异步)很快就会停止响应或抛出异常。有人知道哪里出了问题吗?示例类:classListenerTest{staticintconnectionNumber=0;staticintport=23456;publicvoidStartListening(boolasync){varlistener=newTcpListener(