delphi gethostbyname取主机的多个IP地址  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.097929000854492 seconds