CPing -- Easily ping a host from Windows 95 and Windows NT

How do I use it?
The CPing::Ping() Member Function
Important things to remember
Little Q 'n' A

What does it do?

    You may not know it, but there is a command built into windows called "ping". You can go to a dos prompt while you're connected to the Internet and type:
    
    ping www.codeguru.com
    
    This command lets you know weather or not that host is up and running. This code gives you that functionality form inside your program. I found a couple of other PING or ICMP articles on codeguru.com, but I failed to get any of them to work under '95. Finding a solution that worked for both 'NT and '95 was hard. After I found it, I figured I'd share it; so I packaged it up into my CPing class.
    Note: None of the actual ping code is mine. My code is just the class wrapper. If I knew who wrote it, I would give him or her full credit. If anyone knows, drop me a line.

How do I use it?

  • Create your MFC project
      Don't tell the app wizard that you want to use Windows Sockets
      Don't #include afxsock.h
      Don't call AfxSocketInit()
  • Copy CPing.cpp and CPing.h into your project directory
  • Insert CPing.cpp into your project
  • In the C++ settings of your project settings remove /Yu"stdafx.h" from project options in both release and debug
  • #include <winsock.h> in your stdafx.h
  • In the linker tab, add WSOCK32.LIB
  • #include "CPing.h" in all the files you wish to reference the CPing class or any objects made from it
  • Construct a CPing object and call the Ping() member function

The CPing::Ping() Member Function

    CPing::Ping

      BOOL Ping( char* strHost );

      Return Value

      Nonzero if the host specified by strHost responds; otherwise 0.

      Parameters

      strHost   A pointer to the null-terminated name or IP Address of the host to ping. If strHost points to an IP Address, it must be in the "XXX.XXX.XXX.XXX" format.

      Remarks

      If the host name specified by strHost cannot be resolved, the function returns 0. A return value of 0 can also mean the host failed to respond.

      Example

      The following example demonstrates the use of CPing::Ping.

      
      // example for CPing::Ping
      
      CPing ping;
      
      //ping the gateway
      BOOL bResult = ping.Ping("192.168.0.254");
      
      if( bResult )
      {
      	//the gateway is working fine
      	//lets ping a couple of sites to see if we can get out.
      	if(ping.Ping("www.codeguru.com") || ping.Ping("www.netscape.com"))
      	{
      		//works just fine
      		AfxMessageBox("Everything is A OK.");
      	}
      	else
      	{
      		//DNS error or we can't get out.
      		AfxMessageBox("Can't ping outside the subnet.  It may be a DNS problem or the ISP's fault.");
      	}
      }
      else
      {
      	//the gateway must be down
      	AfxMessageBox("The gateway or router is down.");
      }
      

Important things to remember

  • This class uses ICMP.DLL. Microsoft wasn't too hot on the idea of making ICMP.DLL. They plan to drop support of it. I don't know if it exists in Windows 98. I don't know if it exists in some versions of '95. I suggest your setup program check for ICMP.DLL and install it in the program directory as needed.

  • This class dynamically loads ICMP.DLL in the constructor. So don't make multiple CPing objects!

  • This class also calls WSAStartup(); in the constructor and WSACleanup(); in the destructor. So don't make multiple CPing objects! Also, If your application calls WSAStartup() and WSACleanup(), I suggest you remark out WSAStartup() and WSACleanup() in my constructor and destructor.

  • Since my class does so much stuff in the constructor and destructor, be very wise about where you declare your CPing object. For example, don't declare it in the middle of a while or for loop. It will constantly construct and destruct CPing objects. The icmp.dll will be popping in and out of memory. It will be starting and stopping Winsock services. Not a good idea. I suggest you place it in your project in a way that it will be constructed and destructed only once.

Little Q 'n' A

    Q. Does it compile under warning level 4?
    A. Yes.

    Q. Will it compile under UNICODE settings?
    A. I don't know. I've never used UNICODE.

    Q. What version of Visual C++ did you use?
    A. Microsoft Visual C++ 5.0

    Q. Will it compile under newer or older versions of Visual C++?
    A. It should compile on version 4.0 and above, but I haven't tested it.

    Q. What version of MFC was this code built with?
    A. Whatever comes with Visual C++ 5.0

    Q. Is the code const correct?
    A. I have no clue what that is. I assume no.

    Q. Does it work under '98?
    A. I don't know. Microsoft may have ditched ICMP.DLL or maybe the kept it for backward compatibility. You may end up having to place ICMP.DLL in the same path as the .exe to get it to work.

    Q. Do I have to use MFC?
    A. No. I didn't reference any of the MFC classes or objects. If you wish to not use MFC, you'll will have to create a StdAfx.h file or take the #include "StdAfx.h" out of my class. You will have to #include <windows.h> and #include <winsock.h>. Even after that, you may have to monkey around with it a bit to get it to work.

Download demo executable file - 67.2 KB

Download demo project - 28.6 KB

Download source - 1.93 KB



Comments

  • doesn't work with WAN

    Posted by darsha on 09/19/2009 01:56am

    it doesn't work with WAN.tried to ping www.google.com it doesn't work. I tested with my office LAN. even in that also if my pc IP is 192.168.1.2 then it ping only in 192.168.1... range only. can't ping 192.168.2.13. why???

    Reply
  • Useful ping ?

    Posted by Karsten0815 on 09/08/2005 03:51am

    IcmpSendEcho uses Port 9995 !!! This port ist raped by worm W32.Sasser (google with "Port 9995" !!!). So this code only works if your firewall has an open Port 9995.

    Reply
  • problem with dial-up

    Posted by Legacy on 05/26/2001 12:00am

    Originally posted by: Igor Rosenberg

    Why it request dial-up connection ? Can I disable it ?

    Reply
  • My ping program based on MFC

    Posted by Legacy on 05/05/2001 12:00am

    Originally posted by: yklim

    where are useful program to ping on the network...
    in this program code is made by RAW_SOCK which introduced internet and use Microsoft.
    please note to it and someone who needed this.
    please contact to me..

    summary:
    input the host name or ip and size of maxinum size of package than it reply to ping result to you..
    it is very compatible.

    someone who develop network programming under Data Link Layer send a request to me, and i reply to it.
    very happy to everyone.

    from korea in kimcheon city

    my home page :
    http://cespc1.kumoh.ac.kr/~yklim

    Reply
  • Some additions to code

    Posted by Legacy on 12/21/2000 12:00am

    Originally posted by: Arkady Frankel

    Code in CPing :
    iaDest.s_addr = inet_addr(strHost);
    if (iaDest.s_addr == INADDR_NONE)
    pHost = gethostbyname(strHost);
    else
    pHost = gethostbyaddr((const char *)&iaDest,
    // Here the problem that Dns return error record
    sizeof(struct in_addr), AF_INET);
    //but this call is not really needed because IP just exist in strHost

    can be modified because gethostbyaddr with IP can return
    NULL , if this IP is not written in HOSTS file.
    Previously I thought that it's because of firewall .
    With help of Jon Snader , author of "Effective TCP/IP
    Programming. 44 tips to Improve Your Network Programs",
    obliged handbook for everyone, writing network programs ,
    which give precious advice and Tip 34 from his book
    I see that the problem in return of error messages
    from DNS servers on routers out of firewall

    I changed the code to next and the problem of HOSTS disappeared
    iaDest.s_addr = inet_addr(strHost);
    if (iaDest.s_addr == INADDR_NONE)
    {
    pHost = gethostbyname(strHost);
    if (pHost == NULL)
    {
    char szText[255] ;
    DWORD dw = WSAGetLastError() ;
    sprintf( szText , "WSA Error = %d" , dw ) ;
    AfxMessageBox( szText ) ;
    return FALSE;
    }
    // Copy the IP address
    dwAddress = (DWORD *)(*pHost->h_addr_list);
    }
    else
    {
    pHost = gethostbyaddr((const char *)&iaDest,
    sizeof(struct in_addr), AF_INET);
    if (pHost == NULL)
    {
    // Copy the IP address
    dwAddress = &iaDest.s_addr ;
    }
    else
    dwAddress = (DWORD *)(*pHost->h_addr_list);
    }
    And now no need in putting IP address into HOSTS file

    Reply
  • Ping and Firewall

    Posted by Legacy on 11/26/2000 12:00am

    Originally posted by: Arkady Frankel

    I tried to use your CPing for resolution and I see
    
    that when I write the URL it's OK , but when I write IP
    gethostbyaddr return NULL and WSAGetLastError return
    11004 ( WSANO_DATA ) - The description of it in MSDN says
    "...An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable".
    As usuall I forgot about HOSTS and when I set the line
    209.10.73.138 www.codeguru.com
    as gethostbyname resolved it.
    Everything start to work as needed.

    The same situation with unused IP's . If I attached some IP
    ( which is not in use up to now ), the CPing always return
    FALSE up to the moment I include the IP and name in the hosts file.

    Reply
  • Win95

    Posted by Legacy on 05/16/2000 12:00am

    Originally posted by: Terry Overton

    The problems in Win95 are due to the fact that it didn't ship with Winsock 2. This is available via download from Microsoft's website.

    To get all of the raw sockets stuff to work in '95, install the Winsock 2 upgrade; #include <winsock2.h>; link to WS2_32.LIB. Note that the WSOCK32.LIB is for Win32s applications. Win9x/NT/2000 should use WS2_32.LIB instead.

    Reply
  • It does not act in WAN

    Posted by Legacy on 11/18/1999 12:00am

    Originally posted by: syhwang

    I tested it with my office lan....

    it works well.

    but I did that
    ping www.altavista.com

    it does not work...

    why?

    Reply
  • Precompiled headers CAN be used with CPing

    Posted by Legacy on 05/27/1999 12:00am

    Originally posted by: Eric Darchis

    The integration procedure says:
    >In the C++ settings of your project settings remove /Yu"stdafx.h" from project options in both release and debug

    If you don't do this, you'll get an error about unexpected end of file while looking for precompiled
    header directive.

    However, if you move #include <stdafx.h> from CPing.h to CPing.cpp (first line), you can use the precompiled
    headers in your project, accelerating the compilation process.

    I have tested this with Visual C++ 5.0.

    Eric Darchis <darchis@pobox.com>

    Reply
  • ICMP can be done with raw sockets

    Posted by Legacy on 04/28/1999 12:00am

    Originally posted by: Earl

    DevStudio 6 ships with a sample "ping" that uses raw sockets to do an icmp echo ping. One advantage of this is it doesn't require you to link to the soon to be obsolete ICMP.DLL. Another advantage is you can do it asynchronously.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds