Results 1 to 4 of 4

Thread: Ping requests

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Ping requests

    Is there an easy way to get results from a ping

    for example I know I could do something like this

    Code:
    if(system("ping www.google.com"))
    {
    do stuff
    }
    else
    {
    do other stuff
    }
    But my question is, how do you check for a failed ping attempt? For example GetLastError() doesn't seem to work, so how can I parse the information? Any ideas? I'm looking for a semi simple way, because I've been reading up on using sockets, ICMP, WSANetwork commands and other methods, but it just seems like there sooo much overhead crap that you need to do for such a simple request. There has to be an easier way to check whether or not a ping request is successful or not.

    Any help would be appreciated!

  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    I found a fairly nice way of doing what I want incase anyone is interested:

    Code:
    bool ping(char *destination, HWND hTest)
    {
        char currentText[MAX_KEY_LENGTH];
        char szBuffer[MAX_KEY_LENGTH];
        WSADATA wsaData;
        WSAStartup(0x0202, &wsaData);
        HANDLE hIcmpFile = IcmpCreateFile();
        hostent* remoteHost;
        remoteHost = gethostbyname(destination);
        
        //Error Checking
        if (WSAGetLastError() != 0)
        {
            _stprintf_s(szBuffer,"The ping request was unsuccessful! \n", "");
            GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
            strcat_s(currentText, szBuffer);
            SetWindowText(hTest, currentText);
    
            WSACleanup();
            return false;
        }
    
        if (hIcmpFile==INVALID_HANDLE_VALUE)
        {
            _stprintf_s(szBuffer,"IcmpCreateFile failed! %s", "");
            GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
            strcat_s(currentText, szBuffer);
            SetWindowText(hTest, currentText);
    
            WSACleanup();
            return false;
        }
     
        IPAddr* ipaddr = reinterpret_cast< IPAddr* > (remoteHost->h_addr_list[0]);
    
        _stprintf_s(szBuffer, "Pinging %s [ %s ] \n\n", destination, inet_ntoa(*reinterpret_cast< in_addr* >(ipaddr)));     
        GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
        strcat_s(currentText, szBuffer);
        SetWindowText(hTest, currentText);
     
        LPVOID ReplyBuffer = (VOID*) malloc(sizeof(ICMP_ECHO_REPLY));
     
        for (int i=0; i<3; i++)
        {
            if (IcmpSendEcho(hIcmpFile, *ipaddr, 0,0,NULL, ReplyBuffer,sizeof(ICMP_ECHO_REPLY),1000)==0)
            {
                _stprintf_s(szBuffer,"Ping Request has failed: %s", "Can't ping request");
                GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
                strcat_s(currentText, szBuffer);
                SetWindowText(hTest, currentText);
                return false;
            }
            else
            {
                PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
                in_addr ipreplied;
                ipreplied.S_un.S_addr=pEchoReply->Address;
     
                _stprintf_s(szBuffer,"Reply from %s \tbytes= %i \ttime= %i ms\n\n", inet_ntoa(ipreplied), pEchoReply->DataSize, pEchoReply->RoundTripTime);
                GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
                strcat_s(currentText, szBuffer);
                SetWindowText(hTest, currentText);
                return true;
            }
            Sleep(500);
        }
     
        free(ReplyBuffer);
        IcmpCloseHandle(hIcmpFile);
        WSACleanup();
    }
    
    void test()
    {
        HWND hTest;
        char currentText[MAX_KEY_LENGTH];
        char szBuffer[MAX_KEY_LENGTH];
    
        pingScreen = CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_LEFT, 200, 200, 600, 400,  hWnd, NULL, hInst, NULL);
    
        //Do a ping Check to confirm you are connected to the internet
        if(ping("www.google.com", pingScreen)== true)
        {
            MessageBox(NULL, TEXT("First Ping works"), TEXT("Success"), MB_OKCANCEL);
    
            if(ping("www.facebook.com", pingScreen)== true)
            {
                MessageBox(NULL, TEXT("Second Ping works"), TEXT("Success"), MB_OKCANCEL);
    
                if(ping("www.yahoo.com", pingScreen)== true)
                {
                    MessageBox(NULL, TEXT("Third Ping works"), TEXT("Success"), MB_OKCANCEL);
                }
                else if(ping("yahoo.com", pingScreen)== false)
                {
                    //MessageBox(NULL, TEXT("Third Ping failed"), TEXT("Error"), MB_OKCANCEL);
                    _stprintf_s(szBuffer,"Third Ping failed %s", "");
                    GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
                    strcat_s(currentText, szBuffer);
                    SetWindowText(hTest, currentText);
                }
            }
            else if(ping("www.facebook.com", pingScreen)== false)
            {
                _stprintf_s(szBuffer,"Second Ping failed %s", "");
                GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
                strcat_s(currentText, szBuffer);
                SetWindowText(hTest, currentText);
            }
        }
        else if(ping("www.google.com", pingScreen)== false)
        {
            _stprintf_s(szBuffer,"First Ping failed %s", "");
            GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
            strcat_s(currentText, szBuffer);
            SetWindowText(hTest, currentText);
        }
        
        CenterWindow(pingScreen, hWnd, 500, 300);
    }

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    New Question.
    The above works fine, and now I'm trying to open up a webpage. The code compiles fine and I get now runtime errors, but it just doesn't do anything aside from tell me the ping is successful. I have it execute this function after it is successful in pinging yahoo.

    Here is the code
    Code:
        BOOL  bResults = FALSE;
    
        HINTERNET hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
        HINTERNET hConnect = WinHttpConnect( hSession, L"www.x-null.net",INTERNET_DEFAULT_HTTP_PORT, 0);
        HINTERNET hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL, NULL, NULL, NULL, 0);
    
        // Send a Request.
        if (hRequest) 
        {
            bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
        }
    
        // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
    Any ideas?

  4. #4

    Default

    Quote Originally Posted by James View Post
    New Question.
    The above works fine, and now I'm trying to open up a webpage. The code compiles fine and I get now runtime errors, but it just doesn't do anything aside from tell me the ping is successful. I have it execute this function after it is successful in pinging yahoo.

    Here is the code
    Code:
        BOOL  bResults = FALSE;
    
        HINTERNET hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
        HINTERNET hConnect = WinHttpConnect( hSession, L"www.x-null.net",INTERNET_DEFAULT_HTTP_PORT, 0);
        HINTERNET hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL, NULL, NULL, NULL, 0);
    
        // Send a Request.
        if (hRequest) 
        {
            bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
        }
    
        // Close any open handles.
        if (hRequest) WinHttpCloseHandle(hRequest);
        if (hConnect) WinHttpCloseHandle(hConnect);
        if (hSession) WinHttpCloseHandle(hSession);
    Any ideas?
    First of all, you're not checking the returns of the three functions you call. All of them can return NULL in which case they fail. Talking about WinHttpOpen and WinHttpConnect.
    Also, I don't think WinHttpOpenRequest will accept all the NULL's you're feeding it. It will probably compile but it won't like it runtime. Found this example on the msdn here:
    PHP Code:
            hRequest WinHttpOpenRequesthConnectL"PUT"
                                          
    L"/writetst.txt"
                                           
    NULLWINHTTP_NO_REFERER
                                           
    WINHTTP_DEFAULT_ACCEPT_TYPES,
                                           
    0); 
    That's all for my first look at it
    =|UWS|=|SA|Vince - Head Serveradmin - mohaa.uwsclan.us (MOH:AA - FT)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •