zoukankan      html  css  js  c++  java
  • delphi局域网Ping各主机方法及设置本地ip方法

    1. 首先引用winsock单元
    
     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'; //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:=50;//超时时间,毫秒
          try
              Result:=IcmpSendEcho(hICMP,FIPAddress,pReqData,
              Length(MyString),nil,pIPE,BufferSize,FTimeOut)>0;
          finally   
              IcmpCloseHandle(hICMP);
              FreeLibrary(hICMPdll);   
              FreeMem(pRevData);   
              FreeMem(pIPE);   
          end;   
    end;
    
    true 能Ping通 否则无此主机。
    
    2. 快速设置本地ip
    
    if not pinghost(ip4) then
            begin
              winstr:='netsh  interface ip set address 本地连接 static  192.168.1.9  255.255. 255.0  192.168.1.1  1';
              WinExec(PChar(winstr),SW_HIDE);
            end;
    

      

  • 相关阅读:
    MyBatis参数传入集合之foreach动态sql
    【mybatis】多次查询缓存的问题
    svn is already locked解决方案
    Python 命令行之旅:使用 docopt 实现 git 命令
    Django 官方推荐的姿势:类视图
    这可能就是你苦苦寻找免费、高颜值、功能强大的 Markdown 编辑器(共5款)
    超级好用的 Java 数据可视化库:Tablesaw
    让你如“老”绅士般编写 Python 命令行工具的开源项目:docopt
    开发 Django 博客文章阅读量统计功能
    使用 Docker 让部署 Django 项目更加轻松
  • 原文地址:https://www.cnblogs.com/hejoy91/p/3527139.html
Copyright © 2011-2022 走看看