Book Image

Hands-On Network Programming with C# and .NET Core

By : Sean Burns
Book Image

Hands-On Network Programming with C# and .NET Core

By: Sean Burns

Overview of this book

The C# language and the .NET Core application framework provide the tools and patterns required to make the discipline of network programming as intuitive and enjoyable as any other aspect of C# programming. With the help of this book, you will discover how the C# language and the .NET Core framework make this possible. The book begins by introducing the core concepts of network programming, and what distinguishes this field of programming from other disciplines. After this, you will gain insights into concepts such as transport protocols, sockets and ports, and remote data streams, which will provide you with a holistic understanding of how network software fits into larger distributed systems. The book will also explore the intricacies of how network software is implemented in a more explicit context, by covering sockets, connection strategies such as Transmission Control Protocol (TCP) and User Datagram Protocol (UDP), asynchronous processing, and threads. You will then be able to work through code examples for TCP servers, web APIs served over HTTP, and a Secure Shell (SSH) client. By the end of this book, you will have a good understanding of the Open Systems Interconnection (OSI) network stack, the various communication protocols for that stack, and the skills that are essential to implement those protocols using the C# programming language and the .NET Core framework.
Table of Contents (26 chapters)
Free Chapter
1
Section 1: Foundations of Network Architecture
6
Section 2: Communicating Over Networks
10
Section 3: Application Protocols and Connection Handling
15
Section 4: Security, Stability, and Scalability
21
Section 5: Advanced Subjects

UDP in C#

Now that we've looked at how to implement TCP in C#, let's take a look at its connectionless counterpart in the suite of transport layer protocols, UDP. By its very nature, the sample client and server we'll be writing will be a fair bit simpler than the TCP in terms of setup code, but we'll be using the same pattern we used in the previous section for defining the behavior of our sample application. So, we'll be transmitting requests and accepting and logging responses between a client and a server.

The difference here, however, is that both the client and the server will be implemented in the exact same way. This is because there is no UdpListener class, because UDP doesn't actively listen for connections. Instead, a UDP server simply accepts in bound packets whenever it is set up to look for a new one. For this reason, we'll only...