2005/10/10 | IP-HostName HostName-IP
类别(语言类学习笔记) | 评论(0) | 阅读(190) | 发表于 10:12
unit Unit2;

interface

uses
Classes,winsock,sysutils,windows;

type
convert = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

implementation
uses unit1;

function GetIP(Name:string) : string;
//根据IP地址获取对方计算机名函数
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe :PHostEnt;
pptr : PaPInAddr;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
phe :=GetHostByName(pchar(Name));
pptr := PaPInAddr(Phe^.h_addr_list);
result:=StrPas(inet_ntoa(pptr^[0]^));
WSACleanup;
end;

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
result:='';
WSACleanup();
end;

procedure convert.Execute;
var i:integer;temp:string;comp:boolean;
begin
temp:=form1.Edit1.Text;
comp:=false;
for i:=1 to length(temp) do
begin
if ((temp[i]>'9') or (temp[i]<'0')) and (temp[i]<>'.') then begin//看是否是IP
form1.Button1.Caption:='终止';
Form1.Edit1.Text:=GetIp(temp);
comp:=true;
Form1.Button1.Caption:='转化';
end;
if comp then break;
end;
if not comp then
begin
form1.Button1.Caption:='终止';
Form1.Edit1.Text:=GetDomainName(temp);
Form1.Button1.Caption:='转化';
end;
end;

end.
0

评论Comments