将某一主机域名解析为IP地址。

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 laza 的 blog


将某一主机域名解析为ip地址。
使用 winsock 单元;
过程如下:
function hosttoip(name: string; var ip: string): boolean;
var
  wsdata : twsadata;
  hostname : array [0..255] of char;
  hostent : phostent;
  addr : pchar;
begin
  wsastartup ($0101, wsdata);
  try
    gethostname (hostname, sizeof (hostname));
    strpcopy(hostname, name);
    hostent := gethostbyname (hostname);
    if assigned (hostent) then
      if assigned (hostent^.h_addr_list) then begin
        addr := hostent^.h_addr_list^;
        if assigned (addr) then begin
          ip := format ('%d.%d.%d.%d', [byte (addr [0]),
          byte (addr [1]), byte (addr [2]), byte (addr [3])]);
          result := true;
        end
        else
          result := false;
      end
      else
        result := false
    else begin
      result := false;
    end;
  finally
    wsacleanup;
  end
end;

 

测试时请在在线状态。

测试代码:
var
ip: string;
dns: string;
begin
  dns := inputbox('输入dns域名', '主机名称:', '');
  if hosttoip(dns, ip) then showmessage(ip);
end;
作者:latfi baran

 


 

本文关键:将某一主机域名解析为IP地址。
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top