.NET for network engineers
By: Marco M. Vidal
Tags: Computer Networking, C Sharp Programming, Net Core, Programming

User experience-oriented applications make extensible use of connectivity technologies to ensure easiness, integration, and fault tolerance.

.NET Core brings inside its guts tools to deal with the main network protocols. Let’s explore some of them in a Console application, to avoid any specific graphic interface or server particularity.

Above all, be asynchronous

In order to an Internet communication to work, a stack of protocols needs to be built, converted into electric pulses, and frequently, travel thousands of miles through optical fiber cables and devices of different companies. That implies that reply time is variable and a lot of things can go wrong in the middle of the process.

To avoid the user interface freezing while it is waiting for any data originated from the network, it is important to use asynchronous features as possible. In C#, it requires the use of the keywords await and async which are present in all the examples.

If you are developing with C# 7.3 (.NET Framework or .NET Core up to 2.1), the data wait must occur. It implies including the following instruction in the Main() method:

On C# 8.0 (.NET Core 3.0 or newer), the Main() method can be asynchronous and return a Task . It is enough to call the desired method with the keyword await .

The code inside the methods named as protocols names can be implemented on graphical interfaces and web application without adaptations, where it will be enough to call the desired method with the keyword await .

ICMP — Internet Control Message Protocol

It is used to test connectivity. It allows us to ensure that a device is active and enabled to communicate with other devices.

Notice that there is no return instruction, denoting this operation does not return values. However, as the operation is asynchronous, we must always declare Task as the return type in the method signature.

In this instance, we are sending an ICMP packet to Google’s primary DNS server pointing to its IPv4 address, received by the SendPingAsync() method as a parameter. It is also possible to pass an IPv6 address or hostname without other changes.

It returns an PingReply object that contains all the requested data. The Status property informs the result — success, timeout, unreachable host, etc — and RoundtripTime , the request time in milliseconds.

DNS — Domain Name System

It translates names into IP addresses. Thanks to DNS, we can access Google as www.google.com instead of typing 172.217.29.227 , for instance.

The resolution is made by the static method Dns.GetHostEntryAsync() . It returns an IPHostEntry object, which contains an IPAddress array with all the bonded addresses to the queried name. To show it all, it is required to iterate it with foreach , as shown in this example.

SMTP — Simple Mail Transfer Protocol

It transmits mail messages from end devices to servers and between servers. SMTP also enables to send messages across multiple providers.

The first thing we must do it is to create the mail message itself with a MailMessage object. It receives the source and destination address in its constructor and should have its subjects and body properties filled latter.

The message will be sent by another object — typed as SmtpClient — which must be surrounded with an using block in order to free the network socket and the memory allocation after the transaction ends, whether it fails or not.

It is important to highlight some points about the code above:

SmtpClient should have passed inside its constructor the mail server’s address and its TCP port

should have passed inside its constructor the mail server’s address and its TCP port The property Credentials receives an NetworkCredential object with the username and password that will be used to authenticate against the server

receives an object with the username and password that will be used to authenticate against the server Commonly, e-mails are sent encrypted so the property EnableSsl must receive true

must receive The mail sending is only committed by the SendMailAsync() method, which receives the message instance created earlier by parameter

HTTP — Hypertext Transfer Protocol

It requests and sends web pages. HTTP is the protocol that browsers use to show web pages in HTML. Currently, it is also heavily used to communicate servers between themselves across REST APIs, using JSON and XML formats.

An HttpClient instance is generated and has its transmission triggered by the GetAsync() method, which receives the requested URL by parameter.

The complete reply is an HttpResponseMessage instance that must have its body extracted asynchronously by the ReadAsStringAsync() method, once it can be very large.

Summary

These are some of the most day-to-day most used network protocols. However, there are others embedded directly inside .NET Core, like FTP, DHCP, LDAP, for instance. Others, that are not in the framework — as POP3, IMAP, SSH, SIP — may be used through third-party libraries obtained via NuGet.

There is still the possibility to make a raw TCP or UDP transmission using the Socket and IPEndPoint classes where the desired protocol has no libraries implemented.

With some protocols knowledge and a lot of ideas, there are infinite possibilities of types of applications and automation that can be made possible.