Delphi .NET Tutorial - Get IP Address

'

Demonstrates how to get the list of IP Addresses

One of the many namespaces in the Base Class Framework is the System.Net namespace. It provides a simple programming interface to many of the protocols found on the network today. One of the classes of the namespace is DNS which provides simple domain name resolution functionality. This is used to convert a domain name to its corresponding ip address.

The following source code demonstrates how to use the DNS object within the System.Net namespace to get all of the IP adresses associated with the current machine.

{$APPTYPE CONSOLE}
program getip;

uses
 System,
 System.Net,
 Borland.Delphi.SysUtils;

var
  strMachineName : String;
  ipHost : IPHostEntry;
  ipAddr : array of IPAddress;
  count  : Integer;
begin
  
  //Get the Host Name
  strMachineName := Dns.GetHostName();
  Console.WriteLine('Host Name: ' + strMachineName);

  //Get the Host by Name
  ipHost := Dns.GetHostByName(strMachineName);

  //You can also query the DNS database for info on a 
  //website like users.chello.be
  //In that case replace the above line as:
  //ipHost := Dns.GetHostByName('users.chello.be')

  //Get the list of addresses associated with the host in an array
  ipAddr := ipHost.AddressList;

  //Enumerate the IP Addresses
  For count := 0 To length(ipAddr) - 1  do

  Console.Write(Format('IP Addresses %d: %s ', [count, ipAddr[count].ToString]));
end.

This tutorial was kindly provided by serge perevoznyk


Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips