📄 44.htm
字号:
<br>
WSACleanup(); <br>
return retval; <br>
} <br>
<br>
<br>
-- <br>
人生在世,如果不能喝酒,不能想女人,连人家欺负到了头上也不能还手,还不如死了算了了 <br>
<br>
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.74.189] <br>
发信人: Christopher (Chris), 信区: Winsock <br>
标 题: Re: 一个不错的Winsocket站点 <br>
发信站: BBS 水木清华站 (Fri Feb 27 10:16:55 1998) <br>
<br>
【 在 Christopher (Chris) 的大作中提到: 】 <br>
例程二:调用ICMP.DLL来Ping另一台计算机。建议用WWW方式浏览。 <br>
<br>
Example - How to Ping Another Machine, ICMP.DLL Method <br>
<br>
This example shows how to "ping" another machine using Microsoft's ICMP.DLL. Thiis DLL is an undocumented API for <br>
sending ICMP echo packets, also called "pings," after the submariner's term for sonar signals. This API works fine and is <br>
present on all current Windows boxes with Microsoft Winsocks, but because it is undocumented, Microsoft claims the right to <br>
replace it at any time. Maybe they will, and maybe they won't, but you should keeep the portability issue and the undocumented <br>
issue in mind. If this bothers you, you might check out the raw sockets method ffor sending ping packets. <br>
<br>
For more information on the ICMP.DLL API, check out sockets.com's ICMP API page.. There is also a ping example <br>
program on MarkG's Win32 page; the following console mode program was distilled from his GUI version. <br>
<br>
<br>
By the way, you might be wondering why the program has to load the ICMP.DLL manuually and get the procedure addresses <br>
manually. I'm not sure of the reasons, but I spent an hour trying to get import libraries and DEF files to work. I can only suggest <br>
that some higher power is dead set against you linking this way. B-) <br>
<br>
<br>
<br>
ping.cpp <br>
<br>
// Borland C++ 5.0: bcc32.cpp ping.cpp <br>
// Visual C++ 5.0: cl ping.cpp wsock32.lib <br>
<br>
#include <iostream.h> <br>
#include <winsock.h> <br>
#include <windowsx.h> <br>
#include "icmpdefs.h" <br>
<br>
int doit(int argc, char* argv[]) <br>
{ <br>
// Check for correct command-line args <br>
if (argc < 2) { <br>
cerr << "usage: ping <host>" << endl; <br>
return 1; <br>
} <br>
<br>
// Load the ICMP.DLL <br>
HANDLE hIcmp = LoadLibrary("ICMP.DLL"); <br>
if (hIcmp == 0) { <br>
cerr << "Unable to locate ICMP.DLL!" << endl; <br>
return 2; <br>
} <br>
<br>
// Look up an IP address for the given host name <br>
struct hostent* phe; <br>
if ((phe = gethostbyname(argv[1])) == 0) { <br>
cerr << "Could not find IP address for " << argv[1] << endl; <br>
return 3; <br>
} <br>
<br>
// Get handles to the functions inside ICMP.DLL that we'll need <br>
typedef HANDLE (WINAPI* pfnHV)(VOID); <br>
typedef BOOL (WINAPI* pfnBH)(HANDLE); <br>
typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD, <br>
PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no? <br>
pfnHV pIcmpCreateFile; <br>
pfnBH pIcmpCloseHandle; <br>
pfnDHDPWPipPDD pIcmpSendEcho; <br>
pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp, <br>
"IcmpCreateFile"); <br>
pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp, <br>
"IcmpCloseHandle"); <br>
pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp, <br>
"IcmpSendEcho"); <br>
if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || <br>
(pIcmpSendEcho == 0)) { <br>
cerr << "Failed to get proc addr for function." << endl; <br>
return 4; <br>
} <br>
<br>
// Open the ping service <br>
HANDLE hIP = pIcmpCreateFile(); <br>
if (hIP == INVALID_HANDLE_VALUE) { <br>
cerr << "Unable to open ping service." << endl; <br>
return 5; <br>
} <br>
<br>
// Build ping packet <br>
char acPingBuffer[64]; <br>
memset(acPingBuffer, '\xAA', sizeof(acPingBuffer)); <br>
PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAllocPtr( <br>
GHND, sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer)); <br>
if (pIpe == 0) { <br>
cerr << "Failed to allocate global ping packet buffer." << endl; <br>
return 6; <br>
} <br>
pIpe->Data = acPingBuffer; <br>
pIpe->DataSize = sizeof(acPingBuffer); <br>
<br>
// Send the ping packet <br>
DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), <br>
acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, <br>
sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000); <br>
if (dwStatus != 0) { <br>
cout << "Addr: " << <br>
int(LOBYTE(LOWORD(pIpe->Address))) << "." << <br>
int(HIBYTE(LOWORD(pIpe->Address))) << "." << <br>
int(LOBYTE(HIWORD(pIpe->Address))) << "." << <br>
int(HIBYTE(HIWORD(pIpe->Address))) << ", " << <br>
"RTT: " << int(pIpe->RoundTripTime) << "ms, " << <br>
"TTL: " << int(pIpe->Options.Ttl) << endl; <br>
} <br>
else { <br>
cerr << "Error obtaining info from ping packet." << endl; <br>
} <br>
<br>
// Shut down... <br>
GlobalFreePtr(pIpe); <br>
FreeLibrary(hIcmp); <br>
return 0; <br>
} <br>
<br>
int main(int argc, char* argv[]) <br>
{ <br>
WSAData wsaData; <br>
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { <br>
return 255; <br>
} <br>
<br>
int retval = doit(argc, argv); <br>
<br>
WSACleanup(); <br>
return retval; <br>
} <br>
<br>
icmpdefs.h <br>
<br>
// Structures required to use functions in ICMP.DLL <br>
<br>
typedef struct { <br>
unsigned char Ttl; // Time To Live <br>
unsigned char Tos; // Type Of Service <br>
unsigned char Flags; // IP header flags <br>
unsigned char OptionsSize; // Size in bytes of options data <br>
unsigned char *OptionsData; // Pointer to options data <br>
} IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION; <br>
<br>
typedef struct { <br>
DWORD Address; // Replying address <br>
unsigned long Status; // Reply status <br>
unsigned long RoundTripTime; // RTT in milliseconds <br>
unsigned short DataSize; // Echo data size <br>
unsigned short Reserved; // Reserved for system use <br>
void *Data; // Pointer to the echo data <br>
IP_OPTION_INFORMATION Options; // Reply options <br>
} IP_ECHO_REPLY, * PIP_ECHO_REPLY; <br>
<br>
<br>
-- <br>
人生在世,如果不能喝酒,不能想女人,连人家欺负到了头上也不能还手,还不如死了算了了 <br>
<br>
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.74.189] <br>
发信人: Christopher (Chris), 信区: Winsock <br>
标 题: Re: 一个不错的Winsocket站点 <br>
发信站: BBS 水木清华站 (Fri Feb 27 10:19:36 1998) <br>
<br>
【 在 Christopher (Chris) 的大作中提到: 】 <br>
例程三:使用Raw Sockets的方法Ping另一台计算机。建议用WWW方式浏览。 <br>
Example - How to Ping Another Machine, Raw Sockets Method <br>
<br>
I have finally come up with a short ping program that uses raw sockets. It's stiill about 3 times longer (365 lines versus 122) than <br>
the ICMP method, but it will continue to work, while the ICMP method might fail to work on Windows 98 and Windows NT <br>
5.0. Also, this version is more flexible. <br>
<br>
This program is split into two major parts: a driver part and a "pinger" part. TThe driver mainly just declares main(), which calls <br>
the functions in the pinger part in the proper sequence. The pinger part is mosttly reusable as-is, although you will probably want <br>
to do things like exchanging the output statements for encoded return values. Thhere is also a separate module for the IP <br>
checksum calculation function because it is not specifically tied to pinging; thhis same algorithm is used in other parts of TCP/IP. <br>
<br>
This program is based on a program in the Win32 SDK. However, hardly any of the original code remains. Also, this version is <br>
a bit smarter, compiles un
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -