Getting the Host name and IP Address of Local Machine:
namespace NKUtilities
{
using System;
using System.Net;
public class DNSUtility
{
public static int Main (string [] args)
{
String strHostName = new String ("");
if (args.Length == 0)
{
strHostName = DNS.GetHostName ();
Console.WriteLine ("Local Machine's Host Name: " + strHostName);
}
else
{
strHostName = args[0];
}
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
return 0;
}
}
}
Getting the Local Machine's IP Address:
using System;
using System.Net;
class Test {
public static void Main() {
IPAddress[] a = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i=0; i<a.Length; i++)
Console.WriteLine ("IpAddr[{0}]={1}",i,a[i]); }
}
For Any Host by using the site address:
using System;
// using the System.Net namespace
using System.Net;
class Class1
{
[STAThread]
static void Main(string[] args)
{
// Request the name
Console.Write("Please type the name of the host: ");
// Store it in 'host'
string host = Console.ReadLine();
try
{
// Get the DNS information
IPHostEntry ipHost = Dns.GetHostByName(host);
// Display the host name
Console.WriteLine("Host name: {0}", ipHost.HostName);
// Store the list of IP adresses
IPAddress[] ipAddr = ipHost.AddressList;
// Loop to actually display the IP
for(int x = 0; x < ipAddr.Length; x++)
{
Console.WriteLine("IP address: {0}", ipAddr[x].ToString());
}
}
// Catch the exception (if host was not found)
catch(System.Net.Sockets.SocketException)
{
Console.WriteLine("Host not found.");
}
}
}
No comments:
Post a Comment