我尝试用.net和linux编写简单的echo服务器代码
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);
}
}
当我使用async / await tcp函数,比如linux上的stream.ReadAsync - .net使用epoll或plain套接字?
1 回答
.net核心使用epoll进行linux上的异步操作,you can see it in runtime sources