zoukankan      html  css  js  c++  java
  • delphi 域名转ip并判断ip是否可以联通

    unit Unit1;
    
    interface
    
    uses
    
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    
    Dialogs, StdCtrls,WinSock;
    
    type
    
    TForm1 = class(TForm)
    
    Button1: TButton;
    
    Button2: TButton;
    
    Edit1: TEdit;
    
    Edit2: TEdit;
    
    procedure Button1Click(Sender: TObject);
    
    procedure Button2Click(Sender: TObject);
    
    private
    
    { Private declarations }
    
    public
    
    { Public declarations }
    
    end;
    
    var
    
    Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    function PingHost(HostIP: String): Boolean;
    
    type
    
    PIPOptionInformation = ^TIPOptionInformation;
    
    TIPOptionInformation = packed record
    
    TTL: Byte;
    
    TOS: Byte;
    
    Flags: Byte;
    
    OptionsSize: Byte;
    
    OptionsData: PChar;
    
    end;
    
    PIcmpEchoReply = ^TIcmpEchoReply;
    
    TIcmpEchoReply = packed record
    
    Address: DWORD;
    
    Status: DWORD;
    
    RTT: DWORD;
    
    DataSize: Word;
    
    Reserved: Word;
    
    Data: Pointer;
    
    Options: TIPOptionInformation;
    
    end;
    
    TIcmpCreateFile = function: THandle; stdcall;
    
    TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
    
    TIcmpSendEcho = function(IcmpHandle:THandle;
    
    DestinationAddress: DWORD;
    
    RequestData: Pointer;
    
    RequestSize: Word;
    
    RequestOptions: PIPOptionInformation;
    
    ReplyBuffer: Pointer;
    
    ReplySize: DWord;
    
    Timeout: DWord
    
    ): DWord; stdcall;
    
    var
    
    hICMP :THandle;
    
    hICMPdll :THandle;
    
    IcmpCreateFile :TIcmpCreateFile;
    
    IcmpCloseHandle :TIcmpCloseHandle;
    
    IcmpSendEcho :TIcmpSendEcho;
    
    pIPE :PIcmpEchoReply;// ICMP Echo reply buffer
    
    FIPAddress :DWORD;
    
    FSize :DWORD;
    
    FTimeOut :DWORD;
    
    BufferSize :DWORD;
    
    pReqData,pRevData:PChar;
    
    MyString:string;
    
    begin
    
    Result :=False;
    
    hICMPdll :=LoadLibrary('icmp.dll');
    
    if hICMPdll=0 then exit;
    
    @ICMPCreateFile :=GetProcAddress(hICMPdll,'IcmpCreateFile');
    
    @IcmpCloseHandle :=GetProcAddress(hICMPdll,'IcmpCloseHandle');
    
    @IcmpSendEcho :=GetProcAddress(hICMPdll,'IcmpSendEcho');
    
    hICMP :=IcmpCreateFile;
    
    if (hICMP=INVALID_HANDLE_VALUE)then exit;
    
    FIPAddress :=inet_addr(PChar(HostIP));
    
    MyString :='Hello,World'; //send data buffer
    
    pReqData :=PChar(MyString);
    
    FSize :=40; //receive data buffer
    
    BufferSize :=SizeOf(TICMPEchoReply)+FSize;
    
    GetMem(pIPE,BufferSize);
    
    FillChar(pIPE^,SizeOf(pIPE^),0);
    
    GetMem(pRevData,FSize);
    
    pIPE^.Data :=pRevData;
    
    FTimeOut :=1000;
    
    try
    
    Result :=IcmpSendEcho(hICMP,FIPAddress,pReqData,
    
    Length(MyString),nil,pIPE,BufferSize,FTimeOut)>0;
    
    finally
    
    IcmpCloseHandle(hICMP);
    
    FreeLibrary(hICMPdll);
    
    FreeMem(pRevData);
    
    FreeMem(pIPE);
    
    end;
    
    end;
    
    function HostToIP(Name: string; var Ip: string): Boolean;
    
    var
    
    wsdata : TWSAData;
    
    hostName : array [0..255] of char;
    
    hostEnt : PHostEnt;
    
    addr : PChar;
    
    begin
    
    WSAStartup ($0101, wsdata);
    
    try
    
    gethostname (hostName, sizeof (hostName));
    
    StrPCopy(hostName, Name);
    
    hostEnt := gethostbyname (hostName);
    
    if Assigned (hostEnt) then
    
    if Assigned (hostEnt^.h_addr_list) then begin
    
    addr := hostEnt^.h_addr_list^;
    
    if Assigned (addr) then begin
    
    IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
    
    byte (addr [1]), byte (addr [2]), byte (addr [3])]);
    
    Result := True;
    
    end
    
    else
    
    Result := False;
    
    end
    
    else
    
    Result := False
    
    else begin
    
    Result := False;
    
    end;
    
    finally
    
    WSACleanup;
    
    end
    
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    
    var
    
    IP:String;
    
    flag:Boolean;
    
    begin
    
    //IP:='123.125.114.118';
    
    IP:=edit2.text;
    
    flag:=PingHost(IP);
    
    if flag=true then
    
    MessageBox(0,'ping1','通路',MB_ICONASTERISK and MB_ICONINFORMATION)
    
    else
    
    MessageBox(0,'ping2','断路',MB_ICONASTERISK and MB_ICONINFORMATION);
    
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    
    var
    
    hqw:string;
    
    begin
    
    HostToIP(edit1.text,hqw);
    
    edit2.text:=hqw;
    
    end;
    
    end.
  • 相关阅读:
    洛谷 P1084 [NOIP2012 提高组] 疫情控制(二分,倍增,贪心)
    洛谷 P2680 [NOIP2015 提高组] 运输计划(二分,树上查分,树上倍增,LCA)
    洛谷 P6858 深海少女与胖头鱼(期望dp)
    洛谷 P2197 【模板】nim 游戏(博弈论)
    洛谷 P1297 [国家集训队]单选错位(期望)
    洛谷 P1288 取数游戏II(博弈论)
    洛谷 P4316 绿豆蛙的归宿(期望)
    P1495 【模板】中国剩余定理(CRT)/曹冲养猪
    【模板】快速乘
    [数论学习笔记]费马小定理、欧拉函数、欧拉定理、欧拉降幂公式
  • 原文地址:https://www.cnblogs.com/yzryc/p/6402056.html
Copyright © 2011-2022 走看看