IPHostEntry
In this chapter you will learn:
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:
Home » C# Tutorial » Network