Networking I 2020
The .NET Frameworks provides classes in the System.net.* namespaces for communicating via standard network protocols.
The following table is the summary of the key components:
TABLE
WebClient | For simple download/upload operations via HTTP/FTP. |
WebRequest/WebResponse | For more control over client-side HTTP/FTP operations |
HttpListener | For writing an HTTP server. |
SmtpClient | For constructing and sending mail messages via SMTP. |
Dns | For converting between domain names and addresses. |
TcpClient/UdpClient/TcpListener/Socket | For direct access to the transport and network layers. |
The System.Net.Socket and System.IO also defines types needed for networking.
Probably, the best and fastest way of getting the picture of network is to answer the question: What happens when we type a URL into a browser?
- Browser contacts the DNS server to fine the IP address of URL.
- DNS returns back IP address of the site.
- Browser opens TCP connection to the web server at PORT 80.
- Browser fetches the HTML code of the page requested.
- Browser renders the HTML.
- Browser terminates the connection when user selects close.
What Step 1 and 2 are doing is Domain Name Resolution. The web address we type is nothing but an alias to an IP address which is readable form. Mapping of domain names and their associated IP address is managed by the Domain Name System (DNS), which is a distributed but hierarchical entity.
Picture source: C# In a Nutshell
IPv4 and IPv6 are the internet protocols applied at the network layer. IPv4 is the most widely used protocol until now and IPv6 is the next generation protocol for the internet.
- IPv4 the fourth version of Internet protocol which uses 32 bit addressing while IPv6 uses 128 bit addressing.
- IPv4 allows 4,294,967,296 unique addresses while IPv6 can hold 340-undecillion (34,000,000,.....), 36 0s after 34,...
- IPv4 has different class types: A, B, C, D, and E. Class A, B, C are the three classes of addresses used on IP networks in common practice. Class D addresses are reserved for multicast. Class E addresses are for research organizations.
- IPv6 addresses are classified into three groups:
- Unicast addresses
A Unicast address acts as an identifier for a single interface. An IPv6 packet sent to a Unicast is delivered to the interface identified by that address. - Multicast addresses
A Multicast address acts as an identifier for a group of interfaces that may belong to the different nodes. An IPv6 packet sent to a multicast address is delivered to the multiple interfaces. - Anycast addresses
Anycast addresses act as an identifiers for a set of interfaces that may belong to the different nodes. An IPv6 packet sent to an Anycast address is delivered to the interfaces identified by the address.
- Unicast addresses
- IPv4 address notation: 192.1.2.96, 239.255.255.255
- IPv6 addresses are denoted by eight groups of hexadecimal quartets separated by colons in between them:
[ EAA3:2010:00D3:0000:0000:0000:0000:4FF2] - Because of the increase in the network population, there is a need of IPv6 protocol:
- Increased address space
- More efficient routing
- Reduced management requirement
- Improved methods to change ISP
- Better mobility support
- Multi-homing
- Security
- Scoped address: link-local, site-local and global-address space
Here is an example of the two protocols:
using System; using System.Net; namespace Networking { class Program { static void Main(string[] args) { IPAddress a1 = new IPAddress(new byte[] { 102, 1, 2, 96 }); IPAddress a2 = IPAddress.Parse("102.1.2.96"); IPAddress a3 = IPAddress.Parse("192.1.2.96"); Console.WriteLine(a1.Equals(a2)); Console.WriteLine(a2.AddressFamily); Console.WriteLine(a3.AddressFamily); IPAddress a4 = IPAddress.Parse("[EAA3:2010:00D3:0000:0000:0000:0000:4FF2]"); Console.WriteLine(a4.AddressFamily); } } }
And the output is:
True InterNetwork InterNetwork InterNetworkV6
The IPAddress class in the System.Net namespace represents an address in either protocol. It has a constructor accepting a byte array, and a static Parse() method accepting a correctly formatted string as we see in the example above.
The TCP and UDP protocols break out each IP address into 65,535 ports, allowing a computer on a single address to run multiple applications, each on its own port. Many applications have standard port assignments: HTTP (80), SMTP (25), and so on.
For more info, on TCP port, here.
For more info on TCP vs. UDP, here.
The TCP and UDP ports from 49152 to 65535 are officially unassigned, so they are available for testing and small-scale deployments.
An IP address and port combination is represented in the .NET Framework by the IPEndPoint class:
namespace Networking { class Program { static void Main(string[] args) { IPAddress a = new IPAddress(new byte[] { 102, 1, 2, 96 }); IPEndPoint ipep = new IPEndPoint(a, 5098); Console.WriteLine(ipep.ToString()); } } }
Output is:
102.1.2.96:5098
Firewalls block ports. In many companies, only a few ports are open: port 80 for unencrypted HTTP and port 443 for secure HTTP (HTTPS).
A Uniform Resource Identifier (URI) is a string used to identify a name or a resource on the Internet. Such identification enables interaction with representations of the resource over a network (typically the World Wide Web, file, or email address) using specific protocols. Schemes specifying a concrete syntax and associated protocols define each URI.
The following picture from the book "C# In a Nutshell" is self explanatory.
The picture below is a diagram of URI scheme categories from wiki
.Schemes in the URL (locator) and URN (name) categories form subsets of URI, and also (generally) disjoint sets. Technically URL and URN function as resource IDs; however, one cannot exactly categorize many schemes as one or the other: we can treat all URIs as names, and some schemes embody aspects of both categories.
The URI syntax consists of a URI scheme name (such as http, ftp, mailto or "file) followed by a colon character, and then by a scheme-specific part. The specifications that govern the schemes determine the syntax and semantics of the scheme-specific part, although the URI syntax does force all schemes to adhere to a certain generic syntax that, among other things, reserves certain characters for special purposes (without always identifying those purposes). The URI syntax also enforces restrictions on the scheme-specific part in order to (for example) provide for a degree of consistency when the part has a hierarchical structure. Percent encoding can add extra information to a URI.
using System; namespace Networking { class Program { static void Main() { Uri uri1 = new Uri("http://bogotobogo.com/"); Uri uri2 = new Uri(uri1, "/CSharp/networking1.php"); Uri uri3 = new Uri("http://bogotobogo.com:80/Sitemap.xml/"); Uri uri4 = new Uri("http://bogotobogo.com/CSharp/networking1.php?good=true"); PrintURI(uri1); PrintURI(uri2); PrintURI(uri3); PrintURI(uri4); } static void PrintURI(Uri uri) { Console.WriteLine("AbsolutePath = {0}", uri.AbsolutePath); Console.WriteLine("AbsoluteUri = {0}", uri.AbsoluteUri); Console.WriteLine("Authority = {0}", uri.Authority); Console.WriteLine("DnsSafeHost = {0}", uri.DnsSafeHost); Console.WriteLine("Host = {0}", uri.Host); Console.WriteLine("HostNameType = {0}", uri.HostNameType); Console.WriteLine("IsAbsoluteUri = {0}", uri.IsAbsoluteUri); Console.WriteLine("IsDefaultPort = {0}", uri.IsDefaultPort); Console.WriteLine("IsFile = {0}", uri.IsFile); Console.WriteLine("IsLoopback = {0}", uri.IsLoopback); Console.WriteLine("IsUnc = {0}", uri.IsUnc); Console.WriteLine("LocalPath = {0}", uri.LocalPath); Console.WriteLine("OriginalString = {0}", uri.OriginalString); Console.WriteLine("PathAndQuery = {0}", uri.PathAndQuery); Console.WriteLine("Port = {0}", uri.Port); Console.WriteLine("Query = {0}", uri.Query); Console.WriteLine("Scheme = {0}", uri.Scheme); Console.WriteLine("Segments = {0}", string.Join(",", uri.Segments)); Console.WriteLine("UserEscaped = {0}", uri.UserEscaped); Console.WriteLine(); } } }
Output from the run is:
AbsolutePath = / AbsoluteUri = http://bogotobogo.com/ Authority = bogotobogo.com DnsSafeHost = bogotobogo.com Host = bogotobogo.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = / OriginalString = http://bogotobogo.com/ PathAndQuery = / Port = 80 Query = Scheme = http Segments = / UserEscaped = False AbsolutePath = /CSharp/networking1.php AbsoluteUri = http://bogotobogo.com/CSharp/networking1.php Authority = bogotobogo.com DnsSafeHost = bogotobogo.com Host = bogotobogo.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = /CSharp/networking1.php OriginalString = http://bogotobogo.com/CSharp/networking1.php PathAndQuery = /CSharp/networking1.php Port = 80 Query = Scheme = http Segments = /,CSharp/,networking1.php UserEscaped = False AbsolutePath = /Sitemap.xml/ AbsoluteUri = http://bogotobogo.com/Sitemap.xml/ Authority = bogotobogo.com DnsSafeHost = bogotobogo.com Host = bogotobogo.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = /Sitemap.xml/ OriginalString = http://bogotobogo.com:80/Sitemap.xml/ PathAndQuery = /Sitemap.xml/ Port = 80 Query = Scheme = http Segments = /,Sitemap.xml/ UserEscaped = False AbsolutePath = /CSharp/networking1.php AbsoluteUri = http://bogotobogo.com/CSharp/networking1.php?good=true Authority = bogotobogo.com DnsSafeHost = bogotobogo.com Host = bogotobogo.com HostNameType = Dns IsAbsoluteUri = True IsDefaultPort = True IsFile = False IsLoopback = False IsUnc = False LocalPath = /CSharp/networking1.php OriginalString = http://bogotobogo.com/CSharp/networking1.php?good=true PathAndQuery = /CSharp/networking1.php?good=true Port = 80 Query = ?good=true Scheme = http Segments = /,CSharp/,networking1.php UserEscaped = False
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization