⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 唉 好麻烦
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, winsock;

type
  TForm1 = class(TForm)
    Radio1: TRadioButton;
    Radio2: TRadioButton;
    IPListview: TListView;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
type CheckType = (Name2IP, IP2Name);
TGetIPThread=class(TThread)
public
  //index:integer;
  check_type:CheckType;
  sIP,hostName:string;
  MyListitem:TListitem;
protected
  procedure Execute; override;
  procedure AddItem;
  Procedure AddResult;
  //procedure SaveToList;
end;
var
  Form1: TForm1;

implementation

{$R *.dfm}

function IsLegalIP(IP:string):boolean;
var
  i, j, l: integer;
  ips: array [1..4] of string;
begin

  i:=1;
  for l:=1 to 4 do ips[l]:='';
  for j:=1 to length(ip) do
    if ip[j]<>'.' then
    begin
      if (ip[j]<'0')or(ip[j]>'9') then
      begin
        //showmessage(ip[j]);
        Result:=false;
        exit;
      end;
      ips[i]:=ips[i]+ip[j]
    end
    else inc(i);

  if (i<>4)
      or((strtoint(ips[1])>255)or(strtoint(ips[1])<0))  //originally is <1
      or((strtoint(ips[2])>255)or(strtoint(ips[2])<0))
      or((strtoint(ips[3])>255)or(strtoint(ips[3])<0))
      or((strtoint(ips[4])>255)or(strtoint(ips[4])<0))
  then Result:= false else Result:= true;

end ;
procedure TGetIPThread.AddItem;
var
  listitem: TListitem;
begin
  with TForm1  do
  begin
      ListItem:=form1.IPListview.Items.Insert(0);
      MyListItem:=ListItem;
      MyListItem.Caption := hostName;
      MyListitem.SubItems.add(sIP);
  end;
end;

procedure TGetIPThread.AddResult;
begin
  with Tform1 do
  begin
      MyListItem.Caption := hostName;
      MyListitem.SubItems[0]:=sIP;
  end;
end;

procedure TGetIPThread.Execute;
var
  WSAData: TWSAData;
  HostEnt: PHostEnt;
  netaddr: u_long;
begin

  synchronize(AddItem);

  WSAStartup(2, WSAData);

  case check_type of
    Name2IP:
    begin
      HostEnt := gethostbyname(PChar(hostName));
      if HostEnt <> nil then
      with HostEnt^ do
      sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])])
      else sIP:='未知';
    end;
    IP2Name:
    begin
      netaddr:=inet_addr(Pchar(sIP));
      HostEnt:=GetHostbyaddr(pchar(@netaddr),30,0);
      if HostEnt <> nil then HostName:=strpas(HostEnt.h_name)
      else HostName:='未知';
    end;
  end;//end of case;

  WSACleanup;

  synchronize(AddResult);
  //synchronize(SaveToList);

end;
procedure TForm1.Button1Click(Sender: TObject);
var GetIPThread:TGetIPThread;
begin

  if Radio1.Checked then   //主机名->IP地址
  begin
    GetIPThread:=TGetIPThread.Create(true);
    GetIPThread.FreeOnTerminate := true;
    GetIPThread.check_type := Name2IP;
    GetIPThread.hostName := Edit1.Text;
    GetIPThread.sIP:='正在查找.....';
    GetIPThread.Resume;
  end
  else
  begin
    if IsLegalIP(Edit1.Text) then  //IP地址->主机名
    begin
      GetIPThread:=TGetIPThread.Create(true);
      GetIPThread.FreeOnTerminate := true;
      GetIPThread.check_type := IP2Name;
      GetIPThread.sIP:=Edit1.Text;
      GetIPThread.hostName := '正在查找.....';
      GetIPThread.Resume;
    end
    else ShowMessage(Edit1.text+'不是合法的IP地址');
  end;
  Edit1.Text := '';
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -