Delphi gethostbyname取主机的多个IP地址


源代码:
----------------------------------------------------------------------------------------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
   ListBox1: TListBox;
   Button1: TButton;
   Edit1: TEdit;
   procedure Button1Click(Sender: TObject);
  private
   { Private declarations }
  public
   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;

   { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//取主机的多个IP地址
procedure TForm1.Button1Click(Sender: TObject);
type
  TinfoList=array[0..20] of PInAddr;
  pInfoList=^TinfoList;
var
  pEnt:PHostEnt;
  p:pInfoList;
  i:integer;
begin
  pEnt:=GetHostByName(pchar(edit1.text));

  if pEnt<>nil then
  begin
   i:=0;
   p:=pInfoList(pent^.h_addr_list);
   while p[i]<>nil do
   begin
   ListBox1.Items.Add(StrPas(inet_ntoa(p[i]^)));

   inc(i);
   end;
  end;
end;

constructor TForm1.Create(AOwner: TComponent);
var
  WSData:TWSAData;
begin
  inherited;
  //初始化Ws2_32.dll
  //版本协商
  if WSAStartup(MakeWord(2,0),WSData)<>0 then
   raise Exception.Create('winsock版本不对!');
end;

destructor TForm1.Destroy;
begin
  WSACleanup;
  inherited;
end;

end.


联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678