IPHostEntry

In this chapter you will learn:

  1. DNS Name and Its IPHostEntry
  2. Get DNS host name
  3. Resolve a Host name

DNS Name and Its IPHostEntry

The static Dns class encapsulates the Domain Name Service. Domain Name Service converts between a raw IP address, such as 66.15.12.87, and a human-friendly domain name. The GetHostAddresses method converts from domain name to IP address (or addresses):

using System;/*from   j  av  a2s . c o  m*/
using System.Net;

class MainClass
{
   public static void Main()
   {
      string hostName = Dns.GetHostName();
      Console.WriteLine("Local hostname: {0}", hostName);
      IPHostEntry myself = Dns.GetHostByName(hostName);
      foreach (IPAddress address in myself.AddressList)
      {
         Console.WriteLine("IP Address: {0}", address.ToString());
      }
   }
}

The code above generates the following result.

Get DNS host name

using System;//  j a  va2s.c  o  m
using System.Net;

class MainClass
{
    public static void Main()
    {
        Console.WriteLine(Dns.GetHostName());
    }
}

The code above generates the following result.

Resolve a Host name

using System;/*  java  2  s  .  c o  m*/
using System.Net;

class MainClass
{
   public static void Main(string[] argv)
   {
      IPHostEntry iphe = Dns.Resolve("62.208.12.1");

      Console.WriteLine("Host name: {0}", iphe.HostName);
      foreach(string alias in iphe.Aliases)
      {
         Console.WriteLine("Alias: {0}", alias);
      }
      foreach(IPAddress address in iphe.AddressList)
      {
         Console.WriteLine("Address: {0}",
                     address.ToString());
      }
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Get IP address from host name
  2. Get Host by IP address
Home » C# Tutorial » Network
Cookie
CredentialCache
HostEntry
IPHostEntry
IP address from host name
IPEndPoint from IPAddress
IPEndPoint Port
IPEndPoint properties
IPHostEntry
IP Address Parser
Predefined IP Address
NetworkCredential
Ping
Udp Client
Socket connection
Socket creation
Socket sendTo
TcpClient
TcpListener
UdpClient Receive
UdpClient Sending
Uri
WebClient
Response Headers
WebProxy
WebRequest