【发布时间】:2018-03-27 16:53:20
【问题描述】:
有一个 TcpListener 类的服务器。它使用 BeginAcceptTcpClient (AsyncCallback, Object) 方法接受传入连接。
代码写在示例MSDN中
public static ManualResetEvent tcpClientConnected =
new ManualResetEvent(false);
public static void DoBeginAcceptTcpClient(TcpListener
listener)
{
while(true)
{
tcpClientConnected.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAcceptTcpClient(
new AsyncCallback(DoAcceptTcpClientCallback),
listener);
tcpClientConnected.WaitOne();
}
}
public static void DoAcceptTcpClientCallback(IAsyncResult ar)
{
TcpListener listener = (TcpListener) ar.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(ar);
Console.WriteLine("Client connected completed");
tcpClientConnected.Set();
while(true)
{
//Receiving messages from the client
}
}
问题在于 DoAcceptTcpClientCallback (IAsyncResult ar) 方法有时 开始在当前线程 (main) 中执行,而不是在新线程中执行,并阻塞它 (main)。因此,无法接收以下连接。 请帮助理解为什么不为此方法创建一个线程
【问题讨论】:
-
您没有向我们展示,但我想
//Receiving messages from the client出于某种原因切换回使用 同步 API 调用。不要那样做。一旦你去异步(以某种不涉及创建线程的方式)你不知道你将在哪个线程上运行 - 所以你应该只运行你调度下一个异步方法和然后返回。 -
首先,你为什么不使用AcceptTcpClientAsync ?它适用于所有受支持的 .NET 版本,使异步编程变得更加容易。
-
第二个
asynchronous不代表different thread。这意味着您的线程不会阻塞等待响应。 IO 操作甚至可能不需要单独的线程,因为 Windows 中的 IO 在驱动程序级别始终是异步的。 API 模拟阻塞,使单线程编程更容易
标签: c# .net multithreading sockets asynchronous