function GetDomainName(Ip: string): string;//根据IP取名称 var pH : PHostent; data : twsadata; ii : dword; begin WSAStartup($101, Data); ii := inet_addr(pchar(ip)); pH := gethostbyaddr(@ii, sizeof(ii), PF_INET); if (ph <> nil) then result := pH.h_name else begin result := '未知的计算机'; end; WSACleanup; end; function GetHostIP(HostName: String): String;//根据名称取IP var buf:pChar; iWsaRet:Integer; Data:WSAData; hostent:PHostEnt; begin Result := ''; iWsaRet := WSAStartup($101,Data); if iWsaRet<>0 then begin ShowMessage('Socket initialize error!'); Exit; end; buf := Allocmem(60); strcopy(buf,PChar(HostName)); if Trim(buf)='' then gethostname(buf,60); hostent := gethostbyname(buf); Freemem(buf,60); if hostent=nil then Exit; Result := inet_ntoa(pinAddr(hostent^.h_addr^)^); WSACleanup(); end;procedure TForm1.Button1Click(Sender: TObject); begin edit2.Text:=GetDomainName(edit1.Text); end;procedure TForm1.Button2Click(Sender: TObject); begin Edit1.Text:=GetHostIP(Edit2.Text); end;
var
pH : PHostent;
data : twsadata;
ii : dword;
begin
WSAStartup($101, Data);
ii := inet_addr(pchar(ip));
pH := gethostbyaddr(@ii, sizeof(ii), PF_INET);
if (ph <> nil) then
result := pH.h_name
else
begin
result := '未知的计算机';
end;
WSACleanup;
end;
function GetHostIP(HostName: String): String;//根据名称取IP
var
buf:pChar;
iWsaRet:Integer;
Data:WSAData;
hostent:PHostEnt;
begin
Result := '';
iWsaRet := WSAStartup($101,Data);
if iWsaRet<>0 then
begin
ShowMessage('Socket initialize error!');
Exit;
end;
buf := Allocmem(60);
strcopy(buf,PChar(HostName));
if Trim(buf)='' then
gethostname(buf,60);
hostent := gethostbyname(buf);
Freemem(buf,60);
if hostent=nil then
Exit;
Result := inet_ntoa(pinAddr(hostent^.h_addr^)^);
WSACleanup();
end;procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text:=GetDomainName(edit1.Text);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text:=GetHostIP(Edit2.Text);
end;