CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  网络通信/分布式开发

Win2000的网络断开是如何检测到的。

楼主blackfish(一天到晚游泳的鱼)2001-11-12 18:58:14 在 Delphi / 网络通信/分布式开发 提问

    在win2000中,只要把网线拔掉,在任务栏就会显示“网络电缆没有插好”,  
  它是怎么检测到的呢?是用API函数实现的吗?谁知道在delphi中如何可以实现  
  此功能,谢谢!!!!  
      blackfish988@sohu.com 问题点数:60、回复次数:6Top

1 楼cobi(我是小新)回复于 2001-11-12 20:05:28 得分 20

uses   Winnet;    
     
  function   isOnLan()   :   boolean;    
  var   conType   :   DWORD;    
  begin    
      conType   :=   INTERNET_CONNECTION_LAN;    
      result   :=   InternetGetConnectedState(@ConTypes,0);    
  end;Top

2 楼redbirdli(火鸟)回复于 2001-11-12 20:31:05 得分 0

GZ:)Top

3 楼okhai(不董)回复于 2001-11-12 21:12:22 得分 0

???我也想知道,不过,好像上面说的是有没有联到互联网,  
  我想要求应该是局域网的连接状态吧。Top

4 楼cobi(我是小新)回复于 2001-11-12 21:20:06 得分 40

最简单的方法是通过ping去判断,但是对于安装了防火墙的机器,这一招就不怎么好用了。  
   
  附:一个比较简单的ping例子  
  unit   Unit1;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      StdCtrls,   winsock;  
   
  type  
      TForm1   =   class(TForm)  
          Memo1:   TMemo;  
          Label1:   TLabel;  
          Edit1:   TEdit;  
          Button1:   TButton;  
          procedure   FormCreate(Sender:   TObject);  
          procedure   FormClose(Sender:   TObject;   var   Action:   TCloseAction);  
          procedure   Button1Click(Sender:   TObject);  
      private  
          {   Private   declarations   }  
          procedure   ShowError(error:   Integer);  
      public  
          {   Public   declarations   }  
      end;  
   
      PIPOptionInformation   =   ^TIPOptionInformation;  
      TIPOptionInformation   =   packed   record  
            TTL:                   Byte;             //   Time   To   Live   (used   for   traceroute)  
            TOS:                   Byte;             //   Type   Of   Service   (usually   0)  
            Flags:               Byte;             //   IP   header   flags   (usually   0)  
            OptionsSize:   Byte;             //   Size   of   options   data   (usually   0,   max   40)  
            OptionsData:   PChar;           //   Options   data   buffer  
      end;  
   
      PIcmpEchoReply   =   ^TIcmpEchoReply;  
      TIcmpEchoReply   =   packed   record  
            Address:               DWord;                                 //   replying   address  
            Status:                 DWord;                                 //   IP   status   value   (see   below)  
            RTT:                       DWord;                                 //   Round   Trip   Time   in   milliseconds  
            DataSize:             Word;                                   //   reply   data   size  
            Reserved:             Word;  
            Data:                     Pointer;                             //   pointer   to   reply   data   buffer  
            Options:               TIPOptionInformation;   //   reply   options  
      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  
      Form1:   TForm1;  
   
  implementation  
   
  {$R   *.DFM}  
   
  const  
      IcmpDLL   =   'icmp.dll';  
   
  var  
      hICMPlib:   HModule;  
      IcmpCreateFile   :   TIcmpCreateFile;  
      IcmpCloseHandle:   TIcmpCloseHandle;  
      IcmpSendEcho:         TIcmpSendEcho;  
      hICMP:   THandle;                                           //   Handle   for   the   ICMP   Calls  
   
  procedure   TForm1.FormCreate(Sender:   TObject);  
  var  
      wsadata:   TWSAData;  
  begin  
      //   initialise   winsock  
      if   WSAStartup($101,wsadata)   <>   0   then   begin  
          ShowMessage('Error   initialising   Winsock');  
          halt;  
      end;  
   
      //   register   the   icmp.dll   stuff  
      hICMPlib   :=   loadlibrary(icmpDLL);  
      if   hICMPlib   <>   null   then   begin  
          @ICMPCreateFile   :=   GetProcAddress(hICMPlib,   'IcmpCreateFile');  
          @IcmpCloseHandle:=   GetProcAddress(hICMPlib,   'IcmpCloseHandle');  
          @IcmpSendEcho:=   GetProcAddress(hICMPlib,   'IcmpSendEcho');  
          if   (@ICMPCreateFile   =   Nil)   or   (@IcmpCloseHandle   =   Nil)   or   (@IcmpSendEcho   =   Nil)   then   begin  
              ShowMessage('Error   loading   dll   functions');  
              halt;  
          end;  
          hICMP   :=   IcmpCreateFile;  
          if   hICMP   =   INVALID_HANDLE_VALUE   then   begin  
              ShowMessage('Unable   to   get   ping   handle');  
              halt;  
          end;  
      end  
      else   begin  
          ShowMessage('Unable   to   register   '   +   icmpDLL);  
          halt;  
      end;  
  end;  
   
  procedure   TForm1.FormClose(Sender:   TObject;   var   Action:   TCloseAction);  
  begin  
      //   Free   icmp.dll  
      IcmpCloseHandle(hICMP);  
      FreeLibrary(hICMPlib);  
      //   free   winsock  
      if   WSACleanup   <>   0   then   ShowMessage('Error   freeing   winsock');  
  end;  
   
  procedure   TForm1.Button1Click(Sender:   TObject);  
  const  
      Size   =   56;  
      TimeOut   =   3000;  
  var  
      Address:   DWord;                                           //   Address   of   host   to   contact  
      HostName,   HostIP:   String;                       //   Name   and   dotted   IP   of   host   to   contact  
      Phe:   PHostEnt;                                             //   HostEntry   buffer   for   name   lookup  
      BufferSize,   nPkts:   Integer;  
      pReqData,   pData:   Pointer;  
      pIPE:   PIcmpEchoReply;                               //   ICMP   Echo   reply   buffer  
      IPOpt:   TIPOptionInformation;                 //   IP   Options   for   packet   to   send  
  begin  
      //   Do   a   lookup  
   
      Address   :=   inet_addr(PChar(Edit1.Text));  
   
      if   (Address   =   INADDR_NONE)   then   begin  
          Phe   :=   GetHostByName(PChar(Edit1.Text));  
          if   Phe   =   Nil   then   ShowError(WSAGetLastError)  
          else   begin  
              Address   :=   longint(plongint(Phe^.h_addr_list^)^);  
              HostName   :=   Phe^.h_name;  
              HostIP   :=   StrPas(inet_ntoa(TInAddr(Address)));  
          end;  
      end  
      else   begin  
          Phe   :=   GetHostByAddr(@Address,   4,   PF_INET);  
          if   Phe   =   Nil   then   ShowError(WSAGetLastError)  
          else   begin  
              HostName   :=   Phe^.h_name;  
              HostIP   :=   StrPas(inet_ntoa(TInAddr(Address)));  
          end;  
      end;  
   
      if   Address   =   INADDR_NONE   then   begin  
          Memo1.Lines.Add('Cannot   resolve   hostname   '   +   Edit1.Text);  
      end  
      else  
      begin  
          Memo1.Lines.Add('Sending   '   +   IntToStr(Size)   +   '   bytes   to   '   +  
              HostName   +   '   ('   +   HostIP   +   ')');  
   
          //   Get   some   data   buffer   space   and   put   something   in   the   packet   to   send  
          BufferSize   :=   SizeOf(TICMPEchoReply)   +   Size;  
          GetMem(pReqData,   Size);  
          GetMem(pData,   Size);  
          GetMem(pIPE,   BufferSize);  
          FillChar(pReqData^,   Size,   $AA);  
          pIPE^.Data   :=   pData;  
   
          //   Finally   Send   the   packet  
          FillChar(IPOpt,   SizeOf(IPOpt),   0);  
          IPOpt.TTL   :=   64;  
          NPkts   :=   IcmpSendEcho(hICMP,   Address,   pReqData,   Size,  
              @IPOpt,   pIPE,   BufferSize,   TimeOut);  
          if   NPkts   =   0   then   ShowError(GetLastError)  
              else  
          begin  
              //ShowMessage('ok');  
              HostIP   :=   StrPas(inet_ntoa(TInAddr(pIPE^.Address)));  
              Memo1.Lines.Add('Received   '   +   IntToStr(pIPE^.DataSize)   +  
                  '   bytes   from   '   +   HostIP   +  
                  '   in   '   +   IntToStr(pIPE^.RTT)   +   '   msecs')  
          end;  
   
          //   Free   those   buffers  
          FreeMem(pIPE);  
          FreeMem(pData);  
          FreeMem(pReqData);  
      end;  
  end;  
   
  procedure   TForm1.ShowError(error:   integer);  
  begin  
      Memo1.Lines.Add('Error:   '   +   IntToStr(error));  
  end;  
   
  end.  
  Top

5 楼blackfish(一天到晚游泳的鱼)回复于 2001-11-13 12:45:52 得分 0

1、当我在uses   段加上Winnet   后,总提示“File   not   found   Winnet.dcu”错误,  
      在帮助里也没有InternetGetConnectedState   ,这是API函数吗?  
  2、使用ping的方法可以暂时解决我的问题。  
      但是如果不知道其他机器的名字或ip,那又怎么检测呢?我看win2000里很快就检测  
  出来了,他应该不会老是去ping别人的机器吧。在插上网线是,网卡的灯会亮,win2000  
  会不会通过网卡的驱动来实现检测的呢?Top

6 楼vjeymyf(vjeymyf)回复于 2001-12-24 19:15:44 得分 0

给分了呀Top

相关问题

  • 有谁知道Win2000的网络断开是如何检测到的。
  • 在java中利用socket如何很快的检测出网络断开?
  • 在java中利用socket如何很快的检测出网络断开?
  • 在java中利用socket如何很快的检测出网络断开?
  • 断开网络
  • tcp/ip网络是面向连接的,当物理层断开后,如何检测网络的通断情况?
  • 用VC如何检测网络状态?就像MSN一样,网络一断开,它马上就可以检测到
  • 关于clientsocket和serversocket的问题!如果任一方网络断开而对方根本检测不到??
  • 断开网络,网络断开
  • 如何检测本地连接的断开状态?

关键词

  • win2000
  • 检测
  • winsock
  • phe
  • hicmplib
  • contypes
  • icmpclosehandle
  • icmpcreatefile
  • tipoptioninformation
  • hicmp

得分解答快速导航

  • 帖主:blackfish
  • cobi
  • cobi

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo