C# 多用户 TCP 服务器

C# multiuser TCP server

正在尝试创建异步 TCP 服务器。 以前我可以用 Begin/End 模式很好地完成这个并像这样处理多个客户端:

class Server
{
    IPEndPoint ipep;
    TcpListener listener;
    bool Running;
    List<Client> clients;

    public Server(string host)
    {
        IPAddress ip = IPAddress.Parse(host);
        ipep = new(ip, 21);
        Running = false;
        clients = new();
    }

    public void Start()
    {
        listener = new(ipep);
        listener.Start();
        Running = true;
        while (Running)
            listener.BeginAcceptTcpClient(Accept, null);
    }

    public void Accept(IAsyncResult ar)
    {
        Client client = new(listener.EndAcceptTcpClient(ar));
        clients.Add(client);
        client.WaitCommands();
    }
}

class Client
{
    TcpClient client;
    NetworkStream stream;

    public Client(TcpClient client)
    {
        this.client = client;
        stream = client.GetStream();
    }

    public void WaitCommands()
    {
        stream.BeginRead(/*some buffer stuff*/, Receive, null);
    }

    public void Receive(IAsyncResult ar)
    {
        stream.EndRead(ar);
        stream.BeginRead(/*again buffer stuff*/, Receive, null);
    }
}

互联网上有很多这样的例子。但是 MSDN 似乎建议改用异步方法,所以我想转换成它。这是我拥有的:

class Server
{
    IPEndPoint ipep;
    TcpListener listener;
    bool Running;
    List<Client> clients;

    public Server(string host)
    {
        IPAddress ip = IPAddress.Parse(host);
        ipep = new(ip, 21);
        Running = false;
        clients = new();
    }

    public async Task Start()
    {
        listener = new(ipep);
        listener.Start();
        Running = true;
        while (Running)
        {
            Client c = await listener.AcceptTcpClientAsync();
            clients.Add(c);
            await c.WaitCommands();
        }
    }
}

class Client
{
    TcpClient client;
    NetworkStream stream;

    public Client(TcpClient client)
    {
        this.client = client;
        stream = client.GetStream();
    }

    public async Task WaitCommands()
    {
        while (true)
        {
            await stream.ReadAsync(/*buffer stuff*/);
        }
    }
}

显然 await c.WaitCommands(); 会阻止其他客户端,因为应用程序卡在 while (true) 循环中并且永远不会再次等待接受。我发现一些 _ = Task.Run(async () => await client.WaitCommands()); 可以解决问题。但据我所知,它从线程池中获取线程,这与 Begin/End 方法不同(或者我错了?这也是问题所在)。

所以问题是

  1. 如何在第二个例子中开始从客户端读取并接受另一个?
  2. 我走的路对吗?或者对于大多数用户计数规模应该使用什么方法(即也许我应该为每个客户端分配一些线程)?

像这样:

using System.Net;
using System.Net.Sockets;

var svr = new Server("127.0.0.1");
await svr.Run();
Console.WriteLine("Goodby, World!");

class Server
{
    IPEndPoint ipep;
    TcpListener listener;
    bool Running;
    List<Client> clients;
    private CancellationTokenSource cts;

    public Server(string host)
    {
        IPAddress ip = IPAddress.Parse(host);
        ipep = new(ip, 1121);
        Running = false;
        clients = new();

        this.cts = new CancellationTokenSource();
    }

    public void Stop()
    {
        Running = false;
        cts.Cancel();
    }
    public async Task Run()
    {
        listener = new(ipep);
        listener.Start();
        Running = true;
        while (Running)
        {
            var c = await listener.AcceptTcpClientAsync(cts.Token);
            var client = new Client(c);
            clients.Add(client);
            var clientTask = client.Run(); //don't await
            clientTask.ContinueWith(t => clients.Remove(client));
        }
    }
}

class Client
{
    TcpClient client;
    NetworkStream stream;

    public Client(TcpClient client)
    {
        this.client = client;
        stream = client.GetStream();
        
    }

    public async Task Run()
    {
        var r = new StreamReader(stream);
        var w = new StreamWriter(stream);
        while (true)
        {
            await w.WriteLineAsync("You are standing in an open field west of a white house, with a boarded front door. There is a small mailbox here.");
            await w.WriteAsync(">");
            await w.FlushAsync();

            var l = await r.ReadLineAsync();
            await w.WriteLineAsync("Invalid command " + l);
        }
    }
}

你可以用来测试

c:\> telnet localhost 1121