.net核心与Linux异步/等待TCP

时间:2018-06-23 16:17:58

标签: .net linux .net-core epoll

我尝试用.net和linux用代码编写简单的回显服务器

    static async Task soc() {
        var listener = new TcpListener(IPAddress.Loopback, 8888);
        listener.Start();
        for (;;) {
            logger.LogInformation("Wait new connetcion");
            using(var client = await listener.AcceptTcpClientAsync())
            {
                logger.LogInformation("Get new connection");
                echo(client);
                client.Close();
            }
        }
    }

    static async void echo(TcpClient client) {
        var buf = new byte[512];
        using(var stream = client.GetStream())
        {
            var i = await stream.ReadAsync(buf, 0, 512);

            if (i < 1) {
                return;
            }

            await stream.WriteAsync(buf, 0, 512);
        }
    }

当我在Linux-.net上使用异步/等待tcp函数(如stream.ReadAsync)时,使用epoll还是普通套接字?

1 个答案:

答案 0 :(得分:2)

.net核心将epoll用于Linux you can see it in runtime sources

上的异步操作