IP address from host name

In this chapter you will learn:

  1. Get IP address from host name
  2. Get Host by IP address

Get IP address from host name

using System;//  j ava2  s  .c om
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
      IPAddress myself = ihe.AddressList[0];

        Console.WriteLine(myself);
    }
}

The code above generates the following result.

Get Host by IP address

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

class MainClass
{
   public static void Main(string[] argv)
   {
      IPAddress test = IPAddress.Parse("64.200.123.1");

      IPHostEntry iphe = Dns.GetHostByAddress(test);

      Console.WriteLine("Information for {0}", test.ToString());

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

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to Create IPEndPoint from IPAddress
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