我有一个 C# 程序,其中有很多(比如说大约一千个)打开的 TcpClient 对象。我想进入一种状态,等待任何这些连接发生某些事情。
我宁愿不为每个连接启动一个线程。
就像是...
while (keepRunning)
{
// Wait for any one connection to receive something.
TcpClient active = WaitAnyTcpClient(collectionOfOpenTcpClients);
// One selected connection has incomming traffic. Deal with it.
// (If other connections have traffic during this function, the OS
// will have to buffer the data until the loop goes round again.)
DealWithConnection(active);
}
附加信息:
TcpClient 对象来自 TcpListener。
目标环境将是 MS .NET 或 Mono-on-Linux。
该协议要求在连接打开时长时间处于空闲状态。