当前位置:首页 > 软件开发 > net
firefox

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

将某一主机域名解析为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;

 ↓相关文章:
© 2006-2008 All Rights Reserved