一个局域网内有20多台机器,如何知道某IP的机器是否开机?用ping的方法我已经知道了,如何用Delphi程序实现??
    用‘机器名’检测我已实现,但是用IP检测的话速度很慢,效率太低,这我确实有点想不通,“机器名”不是还要转成IP吗,为什么反而还慢了????

解决方案 »

  1.   

    用Indy控件:Indy Clients面板下面的IdEcho。
      

  2.   

    uses
    WinSock;Function sendarp(ipaddr:ulong;
    temp:dword;
    ulmacaddr:pointer;
    ulmacaddrleng:pointer) : DWord; StdCall; External 'Iphlpapi.dll' Name 'SendARP';procedure TForm1.Button1Click(Sender: TObject);
    var
    myip:ulong;
    mymac:array[0..5] of byte;
    mymaclength:ulong;
    r:integer;
    begin
    myip:=inet_addr(PChar('192.168.6.180'));
    mymaclength:=length(mymac);
    r:=sendarp(myip,0,@mymac,@mymaclength);
    label1.caption:='errorcode:'+inttostr(r);
    label2.caption:=format('%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x',[mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]]);
    end;上面的代码看r就行了,r=0表示成功把这个ip地址转为mac,也就是这个ip开机了,不是0就没有开机
    速度比ping快多了
    不过不同网段的好像不可以这样
      

  3.   

    struct sockaddr_in stationaddr;
    stationaddr.sin_family = AF_INET;
    stationaddr.sin_port = htons(LocalListenPort);
    stationaddr.sin_addr.S_un.S_addr = htonl(m_stationinfo.IP);
    HOSTENT *pResult = gethostbyaddr((const char *)&(stationaddr.sin_addr),4,AF_INET);
    if(pResult==NULL && !bManual)
    {
    int nErrorCode = WSAGetLastError(); }
    else //网络正常
    { }这是VC的
    你改一改吧
      

  4.   

    Angel:为什么返回总是r<>0?   
       如:156.20.23.123
       返回:errorcode:87
           40:00:E8:09:43:00
        
      

  5.   

    同网段? 
    还有156.20.23.123 是lan里面的ip?这个是公网的吧,要用lan内部ip
    如192.168.0.xx
    我在我的lan试过的
      

  6.   

    看自带例子,Indy 有一个实现Ping 的操作
      

  7.   

    uses winsock
    以下是转贴:
         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;
                          ResquestData : Pointer;RequestSize : Word;
                          RequestOption : PIPOptionInformation;
                          ReplyBuffer  : Pointer; ReplySize : DWord;
                          TimeOut : DWord) : DWord;stdcall;
      

  8.   

    TMyPing = class(TForm)
        ExeBtn: TBitBtn;
        Panel1: TPanel;
        PingEdit: TEdit;
        StatusShow: TMemo;
        procedure ExeBtnClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        hICMP: THANDLE;
        IcmpCreateFile : TIcmpCreateFile;
        IcmpCloseHandle: TIcmpCloseHandle;
        IcmpSendEcho: TIcmpSendEcho;
      public
        { Public declarations }
      end;var
      MyPing: TMyPing;implementation{$R *.dfm}procedure TMyPing.FormCreate(Sender: TObject);
    var
      WSAData : TWSAData;
      hICMPdll : HMODULE;
    begin
      WSAStartup($101,WSAData);
      //Load the icmp.dll stuff
      hICMPdll := LoadLibrary('icmp.dll');
      @ICMPCreateFile := GetProcAddress(hICMPdll,'IcmpCreateFile');
      @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
      @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');  hICMP := IcmpCreateFile;
      StatusShow.Text := '';
      StatusShow.Lines.Add(' 目的IP地址      字节数    返回时间(毫秒)');
    end;procedure TMyPing.ExeBtnClick(Sender: TObject);
    var
      IPOpt:TIPOptionInformation;// IP Options for packet to send
      FIPAddress:DWORD;
      pReqData,pRevData:PChar;
      pIPE:PIcmpEchoReply;// ICMP Echo reply buffer
      FSize: DWORD;
      MyString:string;
      FTimeOut:DWORD;
      BufferSize:DWORD;
      cnt : integer;
    begin
      if PingEdit.Text <> '' then
      begin
        FIPAddress := inet_addr(PChar(PingEdit.Text));
        FSize := 40;
        BufferSize := SizeOf(TICMPEchoReply) + FSize;
        GetMem(pRevData,FSize);
        GetMem(pIPE,BufferSize);
        FillChar(pIPE^, SizeOf(pIPE^), 0);
        pIPE^.Data := pRevData;
        MyString := 'Hello,World';
        pReqData := PChar(MyString);
        FillChar(IPOpt, Sizeof(IPOpt), 0);
        IPOpt.TTL := 64;
        FTimeOut := 1000;
        for cnt := 0 to 10 do
        begin
          try
            IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE, BufferSize, FTimeOut);
            if pReqData^ = pIPE^.Options.OptionsData^ then
              StatusShow.Lines.Add('  '+ PChar(PingEdit.Text) + '          ' + IntToStr(pIPE^.DataSize)
                  + '             ' +IntToStr(pIPE^.RTT));
          except
            StatusShow.Lines.Add('Cant resolve host!');
            FreeMem(pRevData);
            FreeMem(pIPE);
            Exit;
          end;
        end;
        FreeMem(pRevData);
        FreeMem(pIPE);
        ICMPCloseHandle(hICMP);
    end;end;
    end.