Thursday, September 12, 2013

C# code to get IPV4 address

Just a code snippet of C# that I'm using to resolve the IPV4 address of a remote server.
using System.Net;
using System.Net.Sockets;


/***************************************************
Resolve the IPV4 address for a host name.  Should work with IP addresses as well.
*/
IPAddress ipAddress = null;
foreach (IPAddress Address in Dns.GetHostAddresses("google.com"))
{
    if (Address.AddressFamily == AddressFamily.InterNetwork)
    {
        ipAddress = Address;                       
        break;
    }
}

No comments:

Post a Comment