delphi菜鸟,刚学delphi,要实现对校园网中的交换机和服务器网络连接情况检测
并检测交换机的cpu使用情况,不知道用什么方法实现,请教高手,在网络上看过很多用ping的例子,都不全,能否赐教,写的详细点!!

解决方案 »

  1.   


    unit ping;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids,  winsock, StdCtrls, ExtCtrls, ComCtrls, MMSystem;type
      PIPOptionInformation = ^TIPOptionInformation;
      TIPOptionInformation = packed record
        TTL: Byte;
        TOS: Byte;
        Flags: Byte;
        OptionsSize: Byte;
        OptionsData: PChar;
      end;
      type 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;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Panel1: TPanel;
        Label1: TLabel;
        Edit1: TEdit;
        Button1: TButton;
        HeaderControl1: THeaderControl;
        CheckTimer: TTimer;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure CheckTimerTimer(Sender: TObject);
      private
        { Private declarations }
      hICMP: THANDLE;
      IcmpCreateFile : TIcmpCreateFile;
      IcmpCloseHandle:TIcmpCloseHandle;
      IcmpSendEcho: TIcmpSendEcho;
      ErrorCount:Integer;
      
      public
        { Public declarations }
      hICMPdll: HMODULE;
      end;var
      Form1: TForm1;
    implementation{$R *.DFM}
    {$R sound.RES}procedure PlayOne;
    begin
        PlaySound(PCHAR('ring'),Hinstance,SND_SYNC or SND_RESOURCE);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      hICMPdll := LoadLibrary('icmp.dll');
      @ICMPCreateFile:= GetProcAddress(hICMPdll, 'IcmpCreateFile');
      @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
      @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
      hICMP := IcmpCreateFile;
      //Memo1.Lines.Add('        ');
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      IPOpt:TIPOptionInformation;
      FIPAddress:DWORD;
      pReqData,pRevData:PChar;
      pIPE:PIcmpEchoReply;
      FSize: DWORD;
      MyString:string;
      FTimeOut:DWORD;
      BufferSize:DWORD;
    begin
      CheckTimer.Enabled:=False;
      if Memo1.Lines.Count>500 then Memo1.Lines.Clear;
      if Edit1.Text <> '' then
      begin
        FIPAddress:=inet_addr(PChar(Edit1.Text));
        if Fipaddress=INADDR_NONE then
         Memo1.Lines.Add('地址无效 Ping32 64')
        else
        begin
            FSize:=80;
            BufferSize:=SizeOf(TICMPEchoReply)+FSize;
            GetMem(pRevData,FSize);
            GetMem(pIPE,BufferSize);
            FillChar(pIPE^, SizeOf(pIPE^), 0);
            pIPE^.Data := pRevData;
            MyString := 'Argen Ping32 Sending Message.';
            pReqData := PChar(MyString);
            FillChar(IPOpt, Sizeof(IPOpt), 0);
            IPOpt.TTL:= 64;
            FTimeOut :=4000;
            IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE,
            BufferSize, FTimeOut);
            try
              try
                if pReqData^ = pIPE^.Options.OptionsData^ then
                begin
                  ErrorCount:=0;
                  Memo1.Lines.Add(inttostr(Buffersize)+'              '+
                    inttoStr(pIPE^.DataSize)+'                    '+IntToStr(pIPE^.RTT));
                end;
              except
                Inc(ErrorCount,1);
                if ErrorCount>=3 then
                  PlayOne;
                Memo1.Lines.Add('系统检测到服务器出现异常!时间:'+DatetimeToStr(Now()));
              end;
            finally
              FreeMem(pRevData);
              FreeMem(pIPE);
            end;
        end;
      end;
      CheckTimer.Enabled:=True;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      icmpclosehandle(hicmp);
      freelibrary(hicmpdll);
    end;procedure TForm1.CheckTimerTimer(Sender: TObject);
    begin
      Button1.OnClick(self);
    end;end.
      

  2.   

    很简单的就是不断的PING 那台的IP地址 看看是否正常 不正常就报警
    至于交换机的CPU 得得看看交换机使用的操作系统是什么
      

  3.   

    用 snmp 哈
    属于网管系统