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

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 验证码: 验证码
查看所有评论
  • 相关文章
  • 广告推荐