Click here to Skip to main content
15,860,972 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 283.1K   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

     
    QuestionAbout the bugs found in this code Pin
    Versy Stems28-Nov-11 22:59
    Versy Stems28-Nov-11 22:59 
    GeneralSome project remarks Pin
    Sergey Kolomenkin19-Sep-07 23:21
    Sergey Kolomenkin19-Sep-07 23:21 
    Generalcannot locate "IPExport.h" file. Pin
    ledallam30-May-07 21:14
    ledallam30-May-07 21:14 
    QuestionMaking it work for Windows Mobile 5.0 [modified] Pin
    bgp200016-Feb-07 6:59
    bgp200016-Feb-07 6:59 
    QuestionDoes this work under WinCE? Pin
    Simon Langdon15-Dec-06 6:00
    Simon Langdon15-Dec-06 6:00 
    AnswerRe: Does this work under WinCE? Pin
    pjnaughter2-Feb-07 0:09
    pjnaughter2-Feb-07 0:09 
    GeneralIPv6 Pin
    apu_be1-Jun-06 3:21
    apu_be1-Jun-06 3:21 
    GeneralRe: IPv6 Pin
    pjnaughter2-Feb-07 0:11
    pjnaughter2-Feb-07 0:11 
    GeneralRTT has always the same value Pin
    isem924-Jan-06 0:36
    isem924-Jan-06 0:36 
    GeneralRawsocket Ping only working as admin Pin
    Joerg Tiedemann14-Jun-05 1:05
    Joerg Tiedemann14-Jun-05 1:05 
    GeneralRe: Rawsocket Ping only working as admin Pin
    jun.guo5-Dec-06 6:16
    jun.guo5-Dec-06 6:16 
    GeneralRe: Rawsocket Ping only working as admin Pin
    jun.guo5-Dec-06 6:17
    jun.guo5-Dec-06 6:17 
    GeneralRe: Rawsocket Ping only working as admin Pin
    Steve Kowald10-Dec-06 5:22
    Steve Kowald10-Dec-06 5:22 
    GeneralCPing can't receive Ping Echo on windowsXP service pack2 Pin
    vark324-Feb-05 17:13
    vark324-Feb-05 17:13 
    GeneralRe: CPing can't receive Ping Echo on windowsXP service pack2 Pin
    pjnaughter24-Feb-05 23:21
    pjnaughter24-Feb-05 23:21 
    GeneralNon-compatible with afxsock.h Pin
    darirod22-Sep-03 4:27
    darirod22-Sep-03 4:27 
    GeneralRe: Non-compatible with afxsock.h Pin
    pjnaughter22-Sep-03 10:08
    pjnaughter22-Sep-03 10:08 
    Generaltime of the ping Pin
    (Steven Hicks)n+16-Jan-03 15:22
    (Steven Hicks)n+16-Jan-03 15:22 
    GeneralRe: time of the ping Pin
    pjnaughter7-Jan-03 12:38
    pjnaughter7-Jan-03 12:38 
    Questionping eg. ftp.download.com fails ? Pin
    Klusen18-Dec-02 5:10
    Klusen18-Dec-02 5:10 
    AnswerRe: ping eg. ftp.download.com fails ? Pin
    pjnaughter18-Dec-02 5:34
    pjnaughter18-Dec-02 5:34 
    GeneralGetLastError Pin
    Klusen11-Dec-02 3:27
    Klusen11-Dec-02 3:27 
    GeneralRe: GetLastError Pin
    pjnaughter11-Dec-02 4:28
    pjnaughter11-Dec-02 4:28 
    GeneralGet IP address using VC++ Pin
    Anonymous29-Oct-02 20:26
    Anonymous29-Oct-02 20:26 
    GeneralRe: Get IP address using VC++ Pin
    pjnaughter30-Oct-02 0:28
    pjnaughter30-Oct-02 0:28 

    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.