当前位置:主页>delphi7/进销存和数据库> 文章内容

怎样检测局域网是否连通?

发布时间:2010-01-29 | QQ免费站
1楼: 请问大家一个问题: 我有一个局域,有几台机器,我如何能够知道某两台机器是可以连通的,这样的代码怎么写呢? 字串3

2楼: 自己写一个ping程序(网上搜索一下,很多) 字串2

3楼: 如果允许ping,那就ping一下。 否则可以连接机器的其他端口,只要连接成功,就说明网络是通的。 还有就是每个机器放一个通信程序,互相发监测消息。

字串3

4楼: 在网上抄一个 ping 程序 。。。。 字串5

5楼: // if IcmpPing(‘127.0.0.1‘) then ShowMessage(‘OK‘) else ShowMessage(‘Error‘); function IcmpPing(IpAddr: string): Boolean; type 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; {$IFNDEF _DLL_}external DLL; {$ENDIF} TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall; {$IFNDEF _DLL_}external DLL; {$ENDIF} TIcmpSendEcho = function( IcmpHandle: THandle; DestinationAddress: DWord; RequestData: Pointer; RequestSize: Word; RequestOptions: PIPOptionInformation; ReplyBuffer: Pointer; ReplySize: DWord; Timeout: DWord ): DWord; stdcall; {$IFNDEF _DLL_}external DLL; {$ENDIF} const Size = 32; TimeOut = 1000; var wsadata: TWSAData; 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 const IcmpDLL = ‘icmp.dll‘; var hICMPlib: HModule; IcmpCreateFile: TIcmpCreateFile; IcmpCloseHandle: TIcmpCloseHandle; IcmpSendEcho: TIcmpSendEcho; hICMP: THandle; // Handle for the ICMP Calls begin // initialise winsock Result := True; if WSAStartup(2, wsadata) <> 0 then begin Result := False; Exit; end; // register the icmp.dll stuff hICMPlib := loadlibrary(icmpDLL); if hICMPlib = 0 then Exit; @ICMPCreateFile := GetProcAddress(hICMPlib, ‘IcmpCreateFile‘); @IcmpCloseHandle := GetProcAddress(hICMPlib, ‘IcmpCloseHandle‘); @IcmpSendEcho := GetProcAddress(hICMPlib, ‘IcmpSendEcho‘); if (@ICMPCreateFile = nil) or (@IcmpCloseHandle = nil) or (@IcmpSendEcho = nil) then begin Result := False; halt; end; hICMP := IcmpCreateFile; if hICMP = INVALID_HANDLE_VALUE then begin Result := False; halt; end; // ------------------------------------------------------------ Address := inet_addr(PChar(IpAddr)); if (Address = DWord(INADDR_NONE)) then begin Phe := GetHostByName(PChar(IpAddr)); if Phe = nil then Result := False 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 Result := False; end; if Address = DWord(INADDR_NONE) then begin Result := False; end; // 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 Result := False; // Free those buffers FreeMem(pIPE); FreeMem(pData); FreeMem(pReqData); // -------------------------------------------------------------- IcmpCloseHandle(hICMP); FreeLibrary(hICMPlib); // free winsock if WSACleanup <> 0 then Result := False; end; 字串9

6楼: 同意chenxz阿,不知道你发消息来还有啥困难?

字串3

7楼: ping 对方的IP。 net send 对方的IP 想说的话。

字串8

8楼: copy 楼上的话回答 :)

字串6

9楼: ping不知道是停留在哪个年头的测试依据。现在的winXP sp2你ping给我看看,:-)
字串5

10楼: 顶 字串6