2006. december 8., péntek

How to get the IP address for a given URL string and the URL string for a given IP address


Problem/Question/Abstract:

How to get the IP address for a given URL string and the URL string for a given IP address

Answer:

Maybe these functions will help you:

uses
  Winsock, { etc. }

function IpAddrToHostName(const sIP: string): string;
var
  I: Integer;
  P: PHostEnt;
begin
  Result := '';
  I := inet_addr(PChar(sIP));
  if I <> u_long(INADDR_NONE) then
  begin
    P := GetHostByAddr(@I, SizeOf(Integer), PF_INET);
    if P <> nil then
      Result := P^.h_name;
  end
  else
    Result := 'Invalid IP address';
end;

function HostNameToIpAddr(const sHost: string): string;
var
  P: PHostEnt;
  Ia: TInAddr;
begin
  Ia.S_addr := 0;
  P := gethostbyname(PChar(sHost));
  if P <> nil then
    with Ia.S_un_b, P^ do
    begin
      s_b1 := h_addr_list^[0];
      s_b2 := h_addr_list^[1];
      s_b3 := h_addr_list^[2];
      s_b4 := h_addr_list^[3];
    end;
  Result := inet_ntoa(ia);
end;

Nincsenek megjegyzések:

Megjegyzés küldése