Click here to Skip to main content
15,861,340 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 
    1) You should state that it is necessary to define either CPING_USE_ICMP or CPING_USE_WINSOCK2 or both of them.
    You could also note in the article that ICMP version works faster and do not need admin rights as stated in Joerg Tiedemann's message before.

    2) I voted 5 inspired by your smtp and pop3 classes, but it was a mistake. This class is rather dirty, has mistakes and usually don't work:

    Here is the new version of code (all following changes were made + some other minor changes):
    http://register.at.tut.by/sources/ping1.3_ex.zip[^]

    a) You should change nPacketSize type from UCHAR to USHORT at least. It can vary from 0 to ~65500.

    b) You should change default value of TTL from 10 to 50 at least (100 is better). Lots of users of internet work with hosts as far as 30 hops away or so. Such users will have your ping not working.
    Note: each IP host forwarding ICMP echo will reduce TTL by one. Ping packet will be dropped by intermediate host when TTL it become zero.

    c) It also could be change TTL and timeout arguments, because more users know and change timeout than TTL.

    d) Your IP_HEADER struct is WRONG!
    Following two members makes MSVC6 to allocate 4 bytes (even with pack(1) enabled):

    unsigned int h_len:4;          // length of the header<br />
    unsigned int version:4;        // Version of IP<br />
    


    Using bit fields in network structure is not correct. (It depends from compiler and computer system).

    Better way is:

    <br />
    	unsigned char VersionAndLen;<br />
    <br />
    	unsigned char h_len() const<br />
    	{<br />
    		return VersionAndLen & 0xF;<br />
    	}<br />


    e) You have some duplicate code checking results between ICMP and RAW_IP versions.

    f) You lose you error codes while closing connection. Here are new pieces of code:

    long le = GetLastError(); // save last error before closesocket()<br />
    //Don't forget to release out socket<br />
    closesocket(sockRaw);<br />
    SetLastError(le);<br />
    


    //Close the ICMP handle<br />
    long le = GetLastError(); // save last error before sm_pIcmpCloseHandle()<br />
      sm_pIcmpCloseHandle(hIP);<br />
      SetLastError(le);<br />
    


    g) wrong usage of MIN_ICMP_PACKET_SIZE and MAX_ICMP_PACKET_SIZE. You should separate cases when working with the body of ICMP packet and with body+ICMP_headers (which are exactly 8 bytes).

    f) Validation of replies is not complete.
    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.