📄 rawping.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html lang="en"><head><title>Winsock Programmer's FAQ: Ping: Raw Sockets Method</title><link rel="Stylesheet" type="text/css" href="../faq.css"></head><body bgcolor="#ffffee" text="#000000" link="#491e00" vlink="#7d2e01" alink="#da7417"><!-- ---- Header Bar ---- --><table border="0" width="95%" bgcolor="#006000" cellpadding="5" cellspacing="3" align="center"> <tr> <td align="left" bgcolor="#e0e0c0"> <font size="2" face=Verdana,Arial,Helvetica> <b><a href="../examples/dllping.html"><<</a></b> </font> </td> <td align="center"> <font face=Verdana,Arial,Helvetica color="#ffffee"> <p align=center class=bigger3><b> Winsock Programmer's FAQ<br> Example: Ping, Raw Sockets Version </b></p> </font> </td> <td align="right" bgcolor="#e0e0c0"> <font size="2" face=Verdana,Arial,Helvetica> <b><a href="../examples/fdpass.html">>></a></b> </font> </td> </tr></table><!-- ---- Body Table ---- --><table width="95%" border="0" cellpadding="10"> <tr valign="top"> <td><p>This pinger uses "raw sockets", so it requires Winsock 2. It'sabout 3 times longer (288 non-comment lines versus 98) than the <ahref="dllping.html">ICMP method</a>, but it will continue to work, whilethe ICMP method might fail to work on future versions of Windows. Thisprogram is also much more flexible than the ICMP.DLL pinger.</p><p>To use this program under Windows NT/2000, you must be logged in asan Administrator.</p><p>This program is split into two major parts: a driver part and a"pinger" part. The driver mainly just declares <code>main()</code>,which calls the functions in the pinger part in the proper sequence. Thepinger part is mostly reusable as-is, although you will probably wantto do things like exchanging the output statements for encoded returnvalues. There is also a separate module for the IP checksum calculationfunction because it is not specifically tied to pinging; this samealgorithm is used in other parts of TCP/IP.</p><p>This program allows you to change the ICMP packet's TTL (time tolive) value. From this, you can do several other interesting things,like developing a <a href="#fn-traceroute">traceroute</a> utility,and finding the <a href="#fn-nexthop">next hop</a> (such as a router)on your network. Notice that we don't use the "<code>ttl</code>" field in<code>struct IPHeader</code>, because can only <i>receive</i> the full IPheader, not send it. Instead, we use the <code>setsockopt()</code> withthe <code>IP_TTL</code> flag to set the TTL option in the IP header.</p><p>You might want to add timeout functionality to this program, so thatit doesn't wait forever for a ping reply. Two ways to handle this areto 1) spin the ping off into a separate thread and handle the timeoutfrom the main thread; or 2) drop in a call to <code>select()</code>before the <code>recvfrom()</code> call, passing something reasonablefor the <i>timeout</i> argument.</p><p>This program is based on a program in the Win32 SDK, though hardlyany of the original code remains. Also, this version is a bit smarter,compiles under both Microsoft and Borland C++, and should be much easierto understand and reuse.</p><hr noshade size=1 color=#404040><h4 class=lmargin><a href="../examples/src/rawping_driver.cpp">rawping_driver.cpp</a></h4><pre><font color="#444444">/*********************************************************************** rawping_driver.cpp - A driver program to test the rawping.cpp module. Building under Microsoft C++ 5.0: cl -GX rawping.cpp rawping_driver.cpp ip_checksum.cpp ws2_32.lib Building under Borland C++ 5.0: bcc32 rawping.cpp rawping_driver.cpp ip_checksum.cpp ws2_32.lib ---------------------------------------------------------------------- Change log: 9/21/1998 - Added TTL support. 2/14/1998 - Polished the program up and separated out the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -