用原始套接字实现ping_在不使用原始套接字的情况下实现PING

用原始套接字实现ping

Windows supports an Internet Control Message Protocol (ICMP) to determine whether or not a particular host is available. ICMP is a network layer protocol that delivers flow control, error messages, routing, and other data between Internet hosts. ICMP is primarily used by application developers for a network ping.

Windows支持Internet控制消息协议(ICMP)来确定特定主机是否可用。 ICMP是网络层协议,可在Internet主机之间传递流控制,错误消息,路由和其他数据。 ICMP主要由应用程序开发人员用于网络ping。

什么是Ping? ( What Is a Ping? )

A ping is the process of sending an echo message to an IP address and reading the reply to verify a connection between TCP/IP hosts. If you are writing a new application, you will be better to use the Winsock 2 raw sockets support, implemented in Indy, for example.

ping是将回显消息发送到IP地址并读取回复以验证TCP / IP主机之间的连接的过程。 如果要编写新的应用程序,则最好使用Winsock 2原始套接字支持,例如在Indy中实现的。

Please note, however, that for Windows NT and Windows 2000 implementations, Raw Sockets are subject to security checks and are accessible only to members of the administrator's group. Icmp.dll provides functionality that allows developers to write Internet ping applications on Windows systems without Winsock 2 support. 

但是请注意,对于Windows NT和Windows 2000实施,原始套接字必须经过安全检查,并且只有管理员组的成员才能访问。 Icmp.dll提供的功能使开发人员无需Winsock 2支持即可在Windows系统上编写Internet ping应用程序。

Note that the Winsock 1.1 WSAStartup function must be called prior to using the functions exposed by ICMP.DLL. If you do not do this, the first call to IcmpSendEcho will fail with error 10091 (WSASYSNOTREADY).

请注意,在使用ICMP.DLL公开的功能之前,必须先调用Winsock 1.1 WSAStartup函数。 如果不这样做,则对IcmpSendEcho的第一次调用将失败,并显示错误10091(WSASYSNOTREADY)。

Below you can find the Ping unit's source code. Here are two examples of usage.

您可以在下面找到Ping单元的源代码。 这是两个用法示例。

示例1:代码段 ( Example 1: Code Snippet )


uses Ping;...​
const
ADP_IP = '208.185.127.40'; (* http://delphi.about.com *)
beginIf
Ping.Ping(ADP_IP) then ShowMessage('About Delphi Programming reachable!');
end
;

示例2:控制台模式Delphi程序 ( Example 2: Console Mode Delphi Program )

Our next example is a console mode Delphi program that uses the Ping unit: . Here's the Ping unit's source:

我们的下一个示例是使用Ping单元的控制台模式Delphi程序 : 这是Ping单元的来源:


unit Ping;​
interfaceuses

Windows, SysUtils, Classes;
type

TSunB = packed record
s_b1, s_b2, s_b3, s_b4: byte;
end
;
TSunW = packed record
s_w1, s_w2: word;
end
;
PIPAddr = ^TIPAddr;
TIPAddr = record
case
integer of
0: (S_un_b: TSunB);1: (S_un_w: TSunW);2: (S_addr: longword);
end
;IPAddr = TIPAddr;
function
IcmpCreateFile : THandle; stdcall; external 'icmp.dll';
function
IcmpCloseHandle (icmpHandle : THandle) : boolean;
stdcall
; external 'icmp.dll'
function
IcmpSendEcho
(IcmpHandle : THandle; DestinationAddress : IPAddr;
RequestData : Pointer; RequestSize : Smallint;
RequestOptions : pointer;
ReplyBuffer : Pointer;
ReplySize : DWORD;
Timeout : DWORD) : DWORD; stdcall; external 'icmp.dll';
function
Ping(InetAddress : string) : boolean;
implementationuses

WinSock;
function
Fetch(var AInput: string;
const
ADelim: string = ' ';
const
ADelete: Boolean = true)
: string;
var

iPos: Integer;
begin
if
ADelim = #0 then begin
// AnsiPos does not work with #0

iPos := Pos(ADelim, AInput);
end else begin

iPos := Pos(ADelim, AInput);
end
;
if
iPos = 0 then begin
Result := AInput;
if
ADelete then begin
AInput := '';
end
;
end else begin

result := Copy(AInput, 1, iPos - 1);
if
ADelete then begin
Delete(AInput, 1, iPos + Length(ADelim) - 1);
end
;
end
;
end
;
procedure
TranslateStringToTInAddr(AIP: string; var AInAddr);
var

phe: PHostEnt;pac: PChar;GInitData: TWSAData;
begin

WSAStartup($101, GInitData);
try

phe := GetHostByName(PChar(AIP));
if
Assigned(phe) thenbegin
pac := phe^.h_addr_list^;
if
Assigned(pac) then
begin
with
TIPAddr(AInAddr).S_un_b do begin
s_b1 := Byte(pac[0]);s_b2 := Byte(pac[1]);s_b3 := Byte(pac[2]);s_b4 := Byte(pac[3]);
end
;
end
else
begin
raise
Exception.Create('Error getting IP from HostName');
end
;
end
else
begin
raise
Exception.Create('Error getting HostName');
end
;
except

FillChar(AInAddr, SizeOf(AInAddr), #0);
end
;WSACleanup;
end
;
function
Ping(InetAddress : string) : boolean;
var

Handle : THandle;
InAddr : IPAddr;
DW : DWORD;
rep : array[1..128] of byte;
begin

result := false;Handle := IcmpCreateFile;
if
Handle = INVALID_HANDLE_VALUE then
Exit;
TranslateStringToTInAddr(InetAddress, InAddr);
DW := IcmpSendEcho(Handle, InAddr, nil, 0, nil, @rep, 128, 0);Result := (DW 0);IcmpCloseHandle(Handle);
end
;​

翻译自: https://www.thoughtco.com/implementing-ping-without-using-raw-sockets-4068869

用原始套接字实现ping

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值