Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4253908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:00:37+00:00 2026-05-21T05:00:37+00:00

I have a problem where I need to determine if the host exists prior

  • 0

I have a problem where I need to determine if the host exists prior to connecting to it. This host does not work with the function gethostbyaddr() because it is not PC-based and does not return host information. It is IP-based only. Whenever I try to call gethostbyaddr() on the IP address, WinSock returns 11004 (WSANODATA).

Is there a similar function (besides ping) to determine if an IP is valid before trying to connect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:00:37+00:00Added an answer on May 21, 2026 at 5:00 am

    I solved the problem by using the built-in Windows API for PING. I changed the gethostbyname() to inet_addr.

    shown here: ICMP.DLL Method

    dllping.cpp

    // Borland C++ 5.0: bcc32.cpp ping.cpp
    // Visual C++ 5.0:  cl ping.cpp wsock32.lib
    //
    // This sample program is hereby placed in the public domain.
    
    #include <iostream.h>
    #include <winsock.h>
    #include <windowsx.h>
    #include "icmpdefs.h"
    
    int doit(int argc, char* argv[])
    {
        // Check for correct command-line args
        if (argc < 2) {
            cerr << "usage: ping <host>" << endl;
            return 1;
        }
    
        // Load the ICMP.DLL
        HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
        if (hIcmp == 0) {
            cerr << "Unable to locate ICMP.DLL!" << endl;
            return 2;
        }
    
        // Look up an IP address for the given host name
        struct hostent* phe;
        if ((phe = gethostbyname(argv[1])) == 0) {
            cerr << "Could not find IP address for " << argv[1] << endl;
            return 3;
        }
    
        // Get handles to the functions inside ICMP.DLL that we'll need
        typedef HANDLE (WINAPI* pfnHV)(VOID);
        typedef BOOL (WINAPI* pfnBH)(HANDLE);
        typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
                PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
        pfnHV pIcmpCreateFile;
        pfnBH pIcmpCloseHandle;
        pfnDHDPWPipPDD pIcmpSendEcho;
        pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp,
                "IcmpCreateFile");
        pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp,
                "IcmpCloseHandle");
        pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp,
                "IcmpSendEcho");
        if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || 
                (pIcmpSendEcho == 0)) {
            cerr << "Failed to get proc addr for function." << endl;
            return 4;
        }
    
        // Open the ping service
        HANDLE hIP = pIcmpCreateFile();
        if (hIP == INVALID_HANDLE_VALUE) {
            cerr << "Unable to open ping service." << endl;
            return 5;
        }
    
        // Build ping packet
        char acPingBuffer[64];
        memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
        PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(
                GMEM_FIXED | GMEM_ZEROINIT,
                sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
        if (pIpe == 0) {
            cerr << "Failed to allocate global ping packet buffer." << endl;
            return 6;
        }
        pIpe->Data = acPingBuffer;
        pIpe->DataSize = sizeof(acPingBuffer);      
    
        // Send the ping packet
        DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), 
                acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, 
                sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
        if (dwStatus != 0) {
            cout << "Addr: " <<
                    int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
                    int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
                    int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
                    int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
                    "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
                    "TTL: " << int(pIpe->Options.Ttl) << endl;
        }
        else {
            cerr << "Error obtaining info from ping packet." << endl;
        }
    
        // Shut down...
        GlobalFree(pIpe);
        FreeLibrary(hIcmp);
        return 0;
    }
    
    int main(int argc, char* argv[])
    {
        WSAData wsaData;
        if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
            return 255;
        }
    
        int retval = doit(argc, argv);
    
        WSACleanup();
        return retval;
    }
    

    icmpdefs.h

    // Structures required to use functions in ICMP.DLL
    
    typedef struct {
        unsigned char Ttl;                         // Time To Live
        unsigned char Tos;                         // Type Of Service
        unsigned char Flags;                       // IP header flags
        unsigned char OptionsSize;                 // Size in bytes of options data
        unsigned char *OptionsData;                // Pointer to options data
    } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION;
    
    typedef struct {
        DWORD Address;                             // Replying address
        unsigned long  Status;                     // Reply status
        unsigned long  RoundTripTime;              // RTT in milliseconds
        unsigned short DataSize;                   // Echo data size
        unsigned short Reserved;                   // Reserved for system use
        void *Data;                                // Pointer to the echo data
        IP_OPTION_INFORMATION Options;             // Reply options
    } IP_ECHO_REPLY, * PIP_ECHO_REPLY;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.