I am writing a socket server and trying to leverage the new async / await
features.
I've based my code off this stackoverflow answer
I get
An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
Is there anything obvious anyone can see in the below code?
public void Main()
{
CancellationTokenSource cts = new CancellationTokenSource();
TcpListener listener = new TcpListener(IPAddress.Any, 6666);
listener.Start();
while (true)
{
AcceptClientsAsync(listener, cts.Token);
}
cts.Cancel();
listener.Stop();
}
async Task AcceptClientsAsync(TcpListener listener, CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
TcpClient client = await listener.AcceptTcpClientAsync();
Console.WriteLine("Client. Doing the do.");
SomeTaskAsync(client, ct);
}
}
async Task SomeTaskAsync(TcpClient client, CancellationToken ct)
{
var stream = client.GetStream();
while (!ct.IsCancellationRequested)
{
// Do Stuff
stream.Close();
client.Close();
}
}