Networking in C#

CsharpProgrammingServer Side Programming

The .NET Framework has a layered, extensible, and managed implementation of networking services. You can easily integrate them into your applications. Use the System.Net; namespace.

Let us see how to acess the Uri class:.In C#, it provides object representation of a uniform resource identifier (URI) −

Uri uri = new Uri("http://www.example.com/");
WebRequest w = WebRequest.Create(uri);

Let us now see the System.Net class. This is used to encorypt connections using using the Secure Socket Layer (SSL). If the URI begins with "https:", SSL is used; if the URI begins with "http:", an unencrypted connection is used.

The following is an example. For SSL with FTP, set the EnableSsl property to true before calling the GetResponse() method.

String uri = "https://www.example.com/";
WebRequest w = WebRequest.Create(uri);

String uriServer = "ftp://ftp.example.com/new.txt"
FtpWebRequest r = (FtpWebRequest)WebRequest.Create(uriServer);
r.EnableSsl = true;
r.Method = WebRequestMethods.Ftp.DeleteFile;

The following is an example showing the usage of System.Net namespace and using the Dns.GetHostEntry, Dns.GetHostName methods and IPHostEntry property AddressList −

Example

using System;
using System.Net;

class Program {
   static void Main() {

      String hostName = string.Empty;
      hostName = Dns.GetHostName();
      Console.WriteLine("Hostname: "+hostName);
      IPHostEntry myIP = Dns.GetHostEntry(hostName);

      IPAddress[] address = myIP.AddressList;

      for (int i = 0; i < address.Length; i++) {
         Console.WriteLine("IP Address {1} : ",address[i].ToString());
      }
      Console.ReadLine();
   }
}
raja
Published on 14-Aug-2018 13:04:27
Advertisements