Recommend book/resources for C# network programming?

Started by
11 comments, last by 8Observer8 4 years, 6 months ago

I'm working on a project that involves 2-way TCP/IP communication between a server and multiple clients over persistent socket connections. It's actually a non-game project but I'm sure the same principles apply, and it is  an area of game-programming I've always wanted to understand better.

I'm working in C# so can anyone recommend a one-volume book on this topic relevant to this language? I could definitely do with learning more about networking generally, but I do want something that covers my particular area - not just a book on networking but one which explains how to accomplish things in C#/.NET effectively.

I do not need it to be a book, but I do want it to be up to date and cohesive... say a series of articles online, that guides me through. I'm an experienced developer and have done quite a lot with C#, but little bare networking code - typically I've done HTTP web requests or used networking middleware that obscures some of the low-level details I need to know now.

Many thanks in advance!

Advertisement

I really recommend a fundamental networking book, like TCP/IP illustrated by Stevens, to understand many of the underlying concepts. Yes, it uses C. It's still a good reference.

THEN you can use books/tutorials that talk about networking-in-C# specifically. Those APIs have gone through three stages: Synchronouse/threaded, Asynchronous/old, and Asynchronous/new. You really want some material that uses the latest asynchronous model. Luckily, the online Microsoft documentation is pretty good for this, once you understand the underlying networking concepts.

For example: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-client-socket-example

https://docs.microsoft.com/en-us/dotnet/csharp/async

enum Bool { True, False, FileNotFound };

I suppose I hoping there was a good reference which has been updated for C# developers, just to have a good one-volume reference?

That MS sample is one everyone throws around and I've seen it all over stack overflow, but I was never sure how good it was. The Async stuff seems a nice implementation but it doesn't seem to make clear how the threading is done under the scenes - how many threads, etc.

For my purpose with a small number of clients I'm undecided if thread-per-client is actually a neater solution - most questions online are about how to get away from that model but if you had say max 10 clients, a dedicated thread handling connection logic is maybe neater? Or would you ALWAYS go the async route regardless?

At some level the 'thread-per-connection' argument becomes academic - your main logic is going to end up being handled in one thread anyway, so it's just a case of deciding where you cross that boundary. And if you have a language or library facility that lets you treat data from the network as events in your main thread, you've solved that problem, and only need to really worry about it if you have a performance issue - which, for 10 non-game connections, you almost certainly do not.

The asynchronous threading in Windows .NET is generally done by a thread pool, which uses an automatically managed number of threads.

The .NET primitives are built on top of the Win32 primitives: OVERLAPPED I/O, GetQueuedIOCompletionStatus(), and CreateThreadpool(). Reading the documentation for how those work, may lead to some more insights about how the .NET implementation presumably works. Reading the open source implementation of .NET Core might also help.

If you're using Mono, then all bets are off, because they appear to use more regular threads, rather than Windows-specific primitives, in their implementation.

enum Bool { True, False, FileNotFound };

First thing would be to have a brief look at the System.IO and System.Net.Sockets documentation on msdn.

From there on, find some demos there and read more carefuly the documentation of the used logic- with details.

I myself do not use asynchronous communcation with distant ends as for server.

On 10/21/2019 at 2:56 PM, Kylotan said:

At some level the 'thread-per-connection' argument becomes academic - your main logic is going to end up being handled in one thread anyway, so it's just a case of deciding where you cross that boundary. And if you have a language or library facility that lets you treat data from the network as events in your main thread, you've solved that problem, and only need to really worry about it if you have a performance issue - which, for 10 non-game connections, you almost certainly do not.

Indeed in my case it is really about "which is simpler to code" rather than "which is more performant". The modern .NET async classes seem (at first glance) to do a great job abstracting this to an asynchronous paradigm without making things much more complicated. 

On 10/22/2019 at 1:39 AM, hplus0603 said:

The asynchronous threading in Windows .NET is generally done by a thread pool, which uses an automatically managed number of threads.

The .NET primitives are built on top of the Win32 primitives: OVERLAPPED I/O, GetQueuedIOCompletionStatus(), and CreateThreadpool(). Reading the documentation for how those work, may lead to some more insights about how the .NET implementation presumably works. Reading the open source implementation of .NET Core might also help.

If you're using Mono, then all bets are off, because they appear to use more regular threads, rather than Windows-specific primitives, in their implementation.

Interesting point on mono :)

 

I assume this is THE book to which @hplus0603 referred originally; I have seen it recommended elsewhere when googling "best TCPIP book" etc: https://www.amazon.co.uk/TCP-IP-Illustrated-Protocols-APC/dp/0201633469

It looks like I will have to buy a used hardback copy which suggests it's not widely used, but might be one of those books I should own in my library almost regardless how much network coding I might do?

I would still love a C#-focused version. I can't find much though I was wondering about something like this: https://www.amazon.co.uk/dp/1789340764 anyone here read it? I Find books still preferable to online documentation for in-depth learning and reading, personally.

It's still very good, but there aren't that many people learning the basics of TCP/IP correctly, so the book may be out of print ?

I know of no better alternative that would be in-print.

enum Bool { True, False, FileNotFound };

I study this book: Multiplayer Game Programming: Architecting Networked Games
Author of the book wrote examples in C++/SDL2 and WinSock. I will translate them in C# and I think it will not be too complicated. I hope. I see that C# has equivalents of the WinSock's functions.

13 hours ago, hplus0603 said:

It's still very good, but there aren't that many people learning the basics of TCP/IP correctly, so the book may be out of print ?

I know of no better alternative that would be in-print.

I see this is actually only volume 1. Are the rest more niche? If I can see a 2nd hand copy at a reasonable price I might pick it up regardless.

This topic is closed to new replies.

Advertisement