I tried this on Windows, and the client appears to continue working, but I want to understand if this behavior is guaranteed or just a fluke:
using (var listener = new TcpListener(IPAddress.Loopback, 34346)) {
listener.Start();
// client
Task.Run(async () => {
using var client = new TcpClient();
await client.ConnectAsync(IPAddress.Loopback, 34346);
await using var stream = client.GetStream();
await Task.Delay(TimeSpan.FromSeconds(30));
stream.WriteByte(42);
});
using var client = await listener.AcceptTcpClientAsync();
listener.Stop();
listener.Dispose();
log.LogInformation("Disposed listener");
await using var stream = client.GetStream();
log.LogInformation("{Byte}", stream.ReadByte());
}
Prints:
Disposed listener
42