Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC
Article

CPing v1.22

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
3 Mar 2000 284K   7.2K   56   61
An MFC class to encapsulate the Ping utility.
  • Download source files - 13 Kb
  • Introduction

    Welcome to CPing, a freeware MFC class to encapsulate the PING protocol.


    Features
    Usage
    History
    API Reference
    Planned Enhancements
    Contacting the Author


    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)
    • Initial public release.

    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


    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralThanks, but doesn't compile Pin
    Jeff4-Sep-00 11:34
    Jeff4-Sep-00 11:34 
    GeneralRe: Thanks, but doesn't compile Pin
    AlexMarbus8-Feb-01 7:37
    AlexMarbus8-Feb-01 7:37 
    GeneralYour ping class is great, but.. Pin
    Member 240725-Jul-00 22:56
    Member 240725-Jul-00 22:56 
    GeneralRe: Your ping class is great, but.. Pin
    25-Feb-01 4:38
    suss25-Feb-01 4:38 
    GeneralTrick to use ATL instead of MFC in CPing Pin
    Benjamin Mayrargue20-Jul-00 7:22
    Benjamin Mayrargue20-Jul-00 7:22 
    GeneralCPing and Threads Pin
    Jeff Proefrock7-Jul-00 9:13
    sussJeff Proefrock7-Jul-00 9:13 
    GeneralRe: CPing and Threads Pin
    pjnaughter9-Jul-00 7:27
    pjnaughter9-Jul-00 7:27 
    GeneralRe: CPing and Threads Pin
    James P24-Jul-00 3:16
    James P24-Jul-00 3:16 
    Also, if you are trying to start the thread using the CPing function it will fail because threads require a C style function and CPing will have a C++ style header.

    Solutions:

    1) Use AfxBeginThread

    2) Make a static function in CPing that takes a pointer to a CPing class as its parameter. That function should call pClassPointer->CPing(parameters).

    You can start your thread with the static function.

    Jame
    GeneralRe: CPing and Threads Pin
    26-Feb-01 23:28
    suss26-Feb-01 23:28 
    GeneralWin 95 Pin
    Anthony29-Jun-00 3:35
    Anthony29-Jun-00 3:35 
    GeneralWin 95 Pin
    Anthony29-Jun-00 3:35
    Anthony29-Jun-00 3:35 
    GeneralRe: Win 95 Pin
    pjnaughter29-Jun-00 10:40
    pjnaughter29-Jun-00 10:40 
    QuestionAdvantage of using Winsock 2 version? Pin
    uniken17-May-00 1:06
    uniken17-May-00 1:06 
    AnswerRe: Advantage of using Winsock 2 version? Pin
    Slay2-Jun-00 8:13
    Slay2-Jun-00 8:13 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.