W
www
Unregistered / Unconfirmed
GUEST, unregistred user!
function GetIPByHost(const HostName: string): string;
var Host: pHostent;
ConsultResult:String;
Err:Integer;
begin
Result := '';
try
Host := GetHostByName(PChar(HostName));
except
Err:=WSAGetLastError();
exit;
end;
if host = nil then begin
exit;
end;
ConsultResult:=Copy(Host.h_addr^,0,4);
Result := Format('%d.%d.%d.%d',[ord(ConsultResult[1]), ord(ConsultResult[2]),
ord(ConsultResult[3]), ord(ConsultResult[4])]);
end;
上面这个函数在解析局域网机器时不对,比如GetIPByHost('server'),得到的是192.168.0.0,实际上应该是192.168.0.2
var Host: pHostent;
ConsultResult:String;
Err:Integer;
begin
Result := '';
try
Host := GetHostByName(PChar(HostName));
except
Err:=WSAGetLastError();
exit;
end;
if host = nil then begin
exit;
end;
ConsultResult:=Copy(Host.h_addr^,0,4);
Result := Format('%d.%d.%d.%d',[ord(ConsultResult[1]), ord(ConsultResult[2]),
ord(ConsultResult[3]), ord(ConsultResult[4])]);
end;
上面这个函数在解析局域网机器时不对,比如GetIPByHost('server'),得到的是192.168.0.0,实际上应该是192.168.0.2