cwoodruff / book-network-programming-csharp

http://csharp-networking.com
Other
34 stars 3 forks source link

Another suggestions #3

Closed mturczyn closed 1 week ago

mturczyn commented 3 months ago

In chapter 5, there's code example

TcpClient client = await listener.AcceptTcpClientAsync();
Console.WriteLine("Client connected.");
await HandleClientAsync(client);

followed by explanation:

This allows the main listening loop to immediately return to waiting for additional client connections, effectively handling multiple concurrent connections without blocking.

This is wrong: await HandleClientAsync(client); waits to handle the client (awaits) and does not proceed to listening again. It will accept new client when it's done with current. Not what is suggested in text. To correct that, it should be var task = HandleClientAsync(client); and then tasks should be awaited later on as needed.

mturczyn commented 3 months ago

Another in chapter 6: there is code snipper with line

Delay = TimeSpan.FromMilliseconds(200), // Wait between each try

and below description states:

with a two-second pause

which is not true, as 200 ms was specified

mturczyn commented 3 months ago

Another inconsistency in chapter 8: in GZip example, we have piece of code:

public byte[] CompressData(byte[] data)
{
    using var outputStream = new MemoryStream();
    using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress))
    {
        gzipStream.Write(data, 0, data.Length);
    }
    return outputStream.ToArray();
}

In Brotli example however, we have:

public static byte[] CompressData(byte[] data)
{
    using var outputStream = new MemoryStream();
    using var brotliStream = new BrotliStream(outputStream, CompressionMode.Compress);
    brotliStream.Write(data, 0, data.Length);
    return outputStream.ToArray();
}

The difference is how using is used with gzipStream and brotliStream - one using is scoped, while the other one is without braces { }. If it's irelevant, i would keep it consistent, but it maybe that the compression stream has to be closed before, that's why first example maybe correct, while Brotli example may not work.