Download source files - 13 Kb
Introduction
Welcome to
CPing
, a freeware MFC class to encapsulate the PING protocol.
Features
- Simple and clean C++ interface.
- The interface provided is synchronous which provides an easier programming model
than using asynchronous sockets.
- A configurable timeout for the connection can be set through the class API.
- The classes are fully Unicode compliant and include Unicode built options in the
workspace file.
Usage
- To use the class in your code simply include ping.cpp in your project and
#include ping.h
in which ever of your modules needs to make calls to the class.
- Your code will need to include MFC either statically or dynamically.
- You will need to have a functioning winsock stack installed as the code links to winsock.dll.
- You will also need to have winsock.h or afxsock.h and afxpriv.h in
your precompiled header. The code will work just aswell in a GUI or console app. The code
should also work in a multithreaded application, although it has not be explicitly tested
in this scenario.
History
V1.0 (11th June 1998)
V1.1 (23rd June 1998)
- The class now supports using Winsock 2 calls instead of using the ICMP dll. To use Winsock
2 calls in
CPing
instead of the ICMP dll, just define the preprocessor constant
CPING_USE_WINSOCK2
and don't forget to link to ws2_32.lib.
V1.2 (30th July 1998)
- Can now use both Winsock 2 calls and ICMP style calls at the same time with the use of
2 preprocessor directives.
- Sample program now use generic entry point
_tmain
.
- Got rid of a 2 level 4 compiler warnings.
- Fixed a problem with the cleanup of ICMP dll usage.
- Tidied up and optimized the usage of static variables.
- VC++ 6.0 project file is now used for the sample so beware.
V1.21 (9th November 1998)
- Now ships with a VC 5 workspace file instead of VC 6. This is mainly because a lot of
people have not migrated to it yet.
- Workspace file now includes configurations to allow both the Winsock 2 and ICMP based
code to be exercised.
- Fixed a level 4 warning which was occurring.
- Removed a number of compiler errors which was occurring when code was compiled to use
the ICMP or Winsock 2.
- Fixed a bug whereby a ping to a non-existent host using the Winsock 2 method blocked
indefinitely.
- Fixed a socket handle leak which was occurring if the Winsock 2 method of pinging
was used.
- Fixed error in API documentation when compared with the actual header file.
- Fixed error in API documentation regarding default timeouts value.
V1.22 (1st March 2000)
- Fixed a problem where I was incorrectly overwritting memory in the function
FillIcmpData()
.
- Tidied up the console info issued by
CPing
.
- Minor changes to the way the demo program is initialized
(
main()
vs. tmain()
etc.).
API Reference
The API consists of the the single public member Ping
of the class CPing
.
- CPing::Ping1
- BOOL CPing::Ping1(LPCTSTR pszHostName, CPingReply& pr, UCHAR nTTL = 10, DWORD dwTimeout = 5000, UCHAR nPacketSize = 32) const;
Return Value:
If the function succeeds, the return value is TRUE. If the function fails, the return value is
FALSE. To get extended error information, call ::GetLastError()
.
Parameters:
- pszHostName -- The network address of the socket to connect to: a machine name such as
"ftp.yourisp.com", or a dotted number such as "128.56.22.8" will both work.
- pr -- This is a reference to a structure which will be filled in upon successful return
of this function. Currently it contains just two members, namely:
- Address -- This is the IP address of the replier.
- RTT -- This is the round trip time in Milliseconds.
- dwTimeout -- This is the timeout to use for connections in milliseconds.
- nTTL -- This is the time to live of the ICMP packet to be sent. For those unfamilar with
the low level details of IP, this is the maximum number of routers through which this
packet should travel. Each time an IP packet goes through a router, its TTL value is
decremented by 1. Eventually when a packet is received with a TTL of 1, it is not
forwarded and instead an ICMP reply is generated. This prevents the network from becoming
flooded with old IP packets. As an aside this is the basis of how traceroute is implemented.
- nPacketSize -- This is the size of the ICMP packet to send.
Remarks:
Internally this function will use the ICMP dll to do a ping. To make this function available you
will need to define the CPING_USE_ICMP
preprocessor macro and link to the winsock
v1.1dll namely wsock32.dll.
- CPing::Ping2
- BOOL CPing::Ping2(LPCTSTR pszHostName, CPingReply& pr, DWORD dwTimeout = 5000, UCHAR nTTL = 10, UCHAR nPacketSize = 32) const;
Return Value:
If the function succeeds, the return value is TRUE. If the function fails, the return value is
FALSE. To get extended error information, call ::GetLastError()
.
Parameters:
- pszHostName -- The network address of the socket to connect to: a machine name such as
"ftp.yourisp.com", or a dotted number such as "128.56.22.8" will both work.
- pr -- This is a reference to a structure which will be filled in upon successful return
of this function. Currently it contains just two members, namely:
- Address -- This is the IP address of the replier.
- RTT -- This is the round trip time in Milliseconds.
- dwTimeout -- This is the timeout to use for connections in milliseconds.
- nTTL -- This is the time to live of the ICMP packet to be sent. For those unfamilar with
the low level details of IP, this is the maximum number of routers through which this
packet should travel. Each time an IP packet goes through a router, its TTL value is
decremented by 1. Eventually when a packet is received with a TTL of 1, it is not
forwarded and instead an ICMP reply is generated. This prevents the network from becoming
flooded with old IP packets. As an aside this is the basis of how traceroute is implemented.
- nPacketSize -- This is the size of the ICMP packet to send.
Remarks:
Internally this function will use the raw winsock 2 calls to do a ping. To make this function
available you will need to define the CPING_USE_WINSOCK2
preprocessor macro and
link to the winsock v2 dll, namely ws2_32.lib. Please note that this dll is only
available on NT or recent service packs of Windows 95 and is not available on the original
build of 95, namely build 950.
Planned Enhancements
- If you have any other suggested improvements, please let me know so that I can incorporate
them into the next release.
Contacting the Author
PJ Naughter
Email: pjn@indigo.ie
Web: http://www.naughter.com
1 March 2000