Network Programming in C# - Part 1

The .NET framework provides two namespaces, System.Net and System.Net.Sockets for network programming. The classes and methods of these namespaces help us to write programs, which can communicate across the network. The communication can be either connection-oriented or connectionless. They can also be either stream-oriented or data-gram-based. The most widely used protocol TCP is used for stream-based communication and UDP is used for data-grams-based applications.

The System.Net.Sockets.Socket is an important class from the System.Net.Sockets namespace. A Socket instance has a local and a remote end-point associated with it. The local end-point contains the connection information for the current socket instance.

There are some other helper classes like IPEndPoint, IPADdress, SocketException, etc, which we can use for Network programming. The .NET framework supports both synchronous and asynchronous communication between the client and server. There are different methods supporting these two types of communication. A synchronous method is operating in blocking mode, in which the method waits until the operation is complete before it returns. However, an asynchronous method operates in non-blocking mode, where it returns immediately, possibly before the operation has completed.

DNS class

The System.net namespace provides this class, which can be used to create and send queries to obtain information about the host server from the Internet Domain Name Service (DNS). Remember that in order to access DNS, the machine executing the query must be connected to a network. If the query is executed on a machine, that does not have access to a domain name server, a System.Net.SocketException is thrown. All the members of this class are static in nature. The important methods of this class are given below.

public static IPHostEntry GetHostByAddress(string address)

Where address should be in a dotted-quad format like "202.87.40.193". This method returns an IPHostEntry instance containing the host information. If the DNS server is not available, the method returns a SocketException.

public static string GetHostName()

This method returns the DNS hostname of the local machine.

In my machine Dns.GetHostName() returns Rajesh which is the DNS name of my machine.

public static IPHostEntry Resolve(string hostname)

This method resolves a DNS hostname or IP address to an IPHostEntry instance. The hostname should be in a dotted-quad format like 127.0.01 or www.microsoft.com.

IPHostEntry class

This is a container class for Internet host address information. This class makes no thread safety guarantees. The following are the important members of this class.

AddressList property

Gives an IPAddress array containing IP addresses that resolve to the hostname.

Aliases property

Gives a string array containing a DNS name that resolves to the IP addresses in the AddressList property.

The following program shows the application of the above two classes.

using System;
using System.Net;
using System.Net.Sockets;

class MyClient
{
    public static void Main()
    {
        IPHostEntry IPHost = Dns.Resolve("www.hotmail.com");
        Console.WriteLine(IPHost.HostName);
        string[] aliases = IPHost.Aliases;
        Console.WriteLine(aliases.Length);
        IPAddress[] addr = IPHost.AddressList;
        Console.WriteLine(addr.Length);
        for (int i = 0; i < addr.Length; i++)
        {
            Console.WriteLine(addr[i]);
        }
    }
}

IPEndPoint Class

This class is a concrete-derived class of the abstract class EndPoint. The IPEndPoint class represents a network endpoint as an IP address and a port number. There are a couple of useful constructors in this class.
IPEndPoint(long addresses, int port).

IPEndPoint endPoint = new IPEndPoint(IPAddress addr, int port);

IPHostEntry IPHost = Dns.Resolve(www.c-sharpcorner.com);
Console.WriteLine(IPHost.HostName);

string[] aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
Console.WriteLine(addr[0]);

EndPoint ep = new IPEndPoint(addr[0], 80);

Conclusion

We already discussed the basics of network programming with C#. We will discuss about the System.Net.Socket class and how we can use the Socket class to write network programming in the coming articles.


Similar Articles