Get Ip address/domain of the domain/ip address in C#.Net

By
Advertisement


Introduction:
In my previous article I have explained about  hoe to Restrict ip,user agent,crawlers in asp.net global.asax. In this article I am going to explain about how to get the ip addess of the domain.

Explanation:
While working in a project I came to the situation like to restrict all the ips except my domain ip. In my previous article I have explained about to restrict particular ips. To get the ip address of the domain include the below namespace  in  code behind.

using System.Net;

After  that  create object for IPHostEntry. IPHostEntry is a container class for internet host address information. Dns.GetHostEntry(HostName) method  will give you the ipaddress of the domain. And
System.Net.IPHostEntry ip = System.Net.Dns.GetHostByAddress("Ip address here"); will give the domain name from the ipaddress.

Below is the code.

static void Main(string[] args)
{
    String HostName = "www.google.com";
    Console.WriteLine("Looking up: {0}", HostName);

    IPHostEntry NameToIpAddress;
    NameToIpAddress = Dns.GetHostEntry(HostName);
    // get ipaddress of the domain from host name
    int AddressCount = 0;
    foreach (IPAddress Address in NameToIpAddress.AddressList)
        Console.WriteLine("IP Address {0}: {1}", ++AddressCount, Address.ToString());
           
    // get domain from host name
    System.Net.IPHostEntry ip = System.Net.Dns.GetHostByAddress("Ip address here");
    Console.WriteLine("IP Address {0}: {1}", ++AddressCount, ip.HostName);           
    Console.ReadLine();
}

Do you like this article. Then comment here or share with your friends. Or like our Facebook page. Or post your comments below


DownloadSource Code Here

4 comments:

  1. Thanks for your information.The below program is useful to check dns look up information :
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;

    class Program
    {
    static void Main(string[] args)
    {
    if (args.Length != 1)
    {
    Console.WriteLine("Usage: DnsLookup hostname/IP Adddress");
    return;
    }

    IPHostEntry ipHostEntry = Dns.GetHostEntry(args[0]);

    Console.WriteLine("Host: {0}", ipHostEntry.HostName);

    if (ipHostEntry.Aliases.Length > 0)
    {
    Console.WriteLine("\nAliases:");
    foreach (string alias in ipHostEntry.Aliases)
    {
    Console.WriteLine(alias);
    }
    }

    Console.WriteLine("\nAddress(es):");
    foreach (IPAddress address in ipHostEntry.AddressList)
    {
    Console.WriteLine("Address: {0}", address.ToString());
    }

    }
    }

    I have checked dns look up information from this site WhoisXY.com

    ReplyDelete

Online Casino