关于根据IP地址得到主机名的问题,请大家帮忙~ ( 积分: 100 )

  • 主题发起人 主题发起人 wanda216
  • 开始时间 开始时间
W

wanda216

Unregistered / Unconfirmed
GUEST, unregistred user!
我在网上看了很多资料,都是千篇一律,这个是他们的地址:http://www.pcppc.cn/kaifa/Delphi/kaifa_118973.html
我怎么编译也编译不出来。请大家帮帮忙,最好把便宜代码发送到我信箱里:42243844@qq.com,谢谢!
 
uese winsock

Function IPToHost(IPAddr : String; Var Host:String): Boolean;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
Result:=False;
WSAStartup($101, WSAData);
try
SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt<>nil then
begin
Host:=StrPas(Hostent^.h_name);
Result:=True;
end
else
begin
Host:='';
Result:=False;
end;
Finally
WSACleanup;
end;
end;

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;
 
后退
顶部