Internet connected or not

Discuss anything else that does not suite other forums.

Moderator: Rathinagiri

User avatar
bpd2000
Posts: 936
Joined: Sat Sep 10, 2011 4:07 am
Location: India
Has thanked: 111 times
Been thanked: 39 times

Internet connected or not

Post by bpd2000 » Sat Jun 17, 2017 9:35 am

Is there any reliable function to know computer is connected with Internet or not
BPD
HMG Convert Dream into Reality

edk
Posts: 125
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 35 times
Been thanked: 65 times

Post by edk » Sat Jun 17, 2017 11:07 am


KDJ
Posts: 159
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 140 times

Post by KDJ » Sun Jun 18, 2017 3:43 pm

This is another way how to detect internet connection:

Code: Select all

#include "hmg.ch"

FUNCTION Main()
  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384702(v=vs.85).aspx
  LOCAL nFlags      := 0
  LOCAL lIsInternet := If(HMG_CallDLL("Wininet.dll", 0, "InternetGetConnectedState", @nFlags, 0) == 1, .T., .F.)

  MsgBox("Is internet: " + HB_ValToStr(lIsInternet) + CRLF + CRLF + "Flags: 0x" + HB_NumToHex(nFlags, 4))

RETURN NIL

User avatar
serge_girard
Posts: 1785
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 241 times
Been thanked: 56 times
Contact:

Post by serge_girard » Sun Jun 18, 2017 3:47 pm

Kryszstof,

Thanks, this would be a great 'standard' HMG function!

Serge

User avatar
bpd2000
Posts: 936
Joined: Sat Sep 10, 2011 4:07 am
Location: India
Has thanked: 111 times
Been thanked: 39 times

Post by bpd2000 » Mon Jun 19, 2017 3:28 am

Kryszstof,

Thank you, but still I am not satisfied, you will get .t. / Internet available even after we switch off the router, subject to LAN cable/status is connected
I am searching for perfect function, I think any function based on PING command will be perfect
BPD
HMG Convert Dream into Reality

User avatar
PeteWG
Posts: 103
Joined: Sun Mar 21, 2010 5:45 pm
Has thanked: 3 times
Been thanked: 23 times

Post by PeteWG » Mon Jun 19, 2017 4:08 pm

bpd2000 wrote:
Mon Jun 19, 2017 3:28 am
I am searching for perfect function, I think any function based on PING command will be perfect
Well, not exactly the "perfect" function ;)
but may want to give it a try...

Code: Select all

/* 
 IsInternet.prg
 Check Internet connection
      NOTE: needs hbtip library. (use hbtip.hbc)
      BUILD: hbmk2 IsInternet hbtip.hbc
*/

PROCEDURE Main()

   ? "IConnect() says: you're " + IF( IConnect(), "connected!", "NOT connected!" ) 
   RETURN NIL
   
FUNCTION IConnect( cUrl )
   LOCAL oCon, oUrl
   LOCAL lRet := .F.   

   hb_Default( @cUrl, "http://www.google.com" )

   oUrl := tURL():New( cUrl )
   oCon := TipClientHttp():New( oUrl )
   oCon:nConnTimeout := 20000
   
   IF ( lRet := oCon:Open( cUrl ) )
      oCon:Close()
   ENDIF
   
   oCon := oUrl := NIL
   
   RETURN lRet

regards,

---
Pete

KDJ
Posts: 159
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 140 times

Post by KDJ » Mon Jun 19, 2017 6:44 pm

Is there an effective method to check the availability of the Internet without using PING?

KDJ
Posts: 159
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 140 times

Post by KDJ » Mon Jun 19, 2017 8:20 pm

This is probably the simplest way with PINGing:

Code: Select all

FUNCTION Main()
  // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384346(v=vs.85).aspx
  LOCAL lIsInternet := If(HMG_CallDLL("Wininet.dll", 0, "InternetCheckConnection", "http://www.google.com", 1 /*FLAG_ICC_FORCE_CONNECTION*/, 0) == 1, .T., .F.)

  MsgBox("Is internet: " + HB_ValToStr(lIsInternet))

RETURN NIL

User avatar
bpd2000
Posts: 936
Joined: Sat Sep 10, 2011 4:07 am
Location: India
Has thanked: 111 times
Been thanked: 39 times

Post by bpd2000 » Tue Jun 20, 2017 4:08 am

Thank you Pete and KDJ
I had tested all the function and found that result from PING command is perfect
I request other user to post their result
BPD
HMG Convert Dream into Reality

User avatar
bpd2000
Posts: 936
Joined: Sat Sep 10, 2011 4:07 am
Location: India
Has thanked: 111 times
Been thanked: 39 times

Post by bpd2000 » Tue Jun 20, 2017 6:16 am

Check Connectivity of Internet based on the result of PING command
You can also use this function to check computer IP in LAN before performing any action to avoid program crash

Code: Select all

/*
Check Internet connectivity through PING command and it is reliable
From bpd2000
Expert help from KDJ
*/


#include "hmg.ch"

FUNCTION MAIN() 
   Local xerrdescription , xurl := "www.google.com"
   
   xerrdescription := HB_PING(xurl)
   Do Case
      Case xerrdescription = 0
       MSGINFO("Ping to " + xurl + " Succesful", "Internet Connection Available")
      Case xerrdescription = 11001
       MSGINFO("The reply buffer was too small.")
      Case xerrdescription = 11002
       MSGINFO("The destination network was unreachable.")
      Case xerrdescription = 11003
       MSGINFO("The destination host was unreachable.")
      Case xerrdescription = 11004
       MSGINFO("The destination protocol was unreachable.")
      Case xerrdescription = 11005
       MSGINFO("The destination port was unreachable.")
      Case xerrdescription = 11006
       MSGINFO("Insufficient IP resources were available.")
      Case xerrdescription = 11007
       MSGINFO("A bad IP option was specified.")
      Case xerrdescription = 11008
       MSGINFO("A hardware error occurred.")
      Case xerrdescription = 11009
       MSGINFO("The packet was too big.")
      Case xerrdescription = 11010
       MSGINFO("The request timed out." + hb_osnewline() +"Internet Connection Not Available")    
      Case xerrdescription = 11011
       MSGINFO("A bad request.")
      Case xerrdescription = 11012
       MSGINFO("A bad route.")
      Case xerrdescription = 11013
       MSGINFO("The time to live (TTL) expired in transit.")
      Case xerrdescription = 11014
       MSGINFO("The time to live expired during fragment reassembly.")
      Case xerrdescription = 11015
       MSGINFO("A parameter problem.")
      Case xerrdescription = 11016
       MSGINFO("Datagrams are arriving too fast to be processed" + hb_osnewline() + ;
       " and datagrams may have been discarded.")
      Case xerrdescription = 11017
       MSGINFO("An IP option was too big.")
      Case xerrdescription = 11018
       MSGINFO("A bad destination.")
      Case xerrdescription = 11050
        MSGINFO("A general failure" + hb_osnewline() + ;
          "This error can be returned for some malformed ICMP packets.")
      Otherwise
        msginfo("Internet Connection Not Available")
    ENDCASE
   RETURN NIL 

//https://groups.google.com/forum/#!topic/harbour-users/Jag2rPxWK_U
#pragma BEGINDUMP 

#include <hbapi.h> 
#include <winsock2.h> 
#include <iphlpapi.h> 
#include <icmpapi.h> 

int hb_Ping( const char * cp ) 
{ 
    HANDLE hIcmpFile; 
    unsigned long ipaddr = INADDR_NONE;     // corrected by KDJ
    DWORD dwRetVal; 
    char SendData[32] = "Data Buffer"; 
    LPVOID ReplyBuffer; 
    DWORD ReplySize; 

    if( isalpha( cp[0] ) )      //host address is a name 
    { 
       WSADATA wsaData; 
       int     iResult; 

       iResult = WSAStartup( MAKEWORD(2, 2), &wsaData ); 

       if( iResult == 0 ) 
       { 
          struct hostent *remoteHost = gethostbyname( cp ); 

          if( remoteHost != NULL ) 
             ipaddr = *(unsigned long *) remoteHost->h_addr_list[0]; 

          WSACleanup(); 
       } 
    } 
    else 
       ipaddr = inet_addr( cp ); 

    if (ipaddr == INADDR_NONE) 
        return 1; 
    
    hIcmpFile = IcmpCreateFile(); 
    if (hIcmpFile == INVALID_HANDLE_VALUE) 
        return 2; 

    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); 
    ReplyBuffer = (VOID*) malloc(ReplySize); 
    if (ReplyBuffer == NULL) 
    { 
        IcmpCloseHandle(hIcmpFile); 
        return 3; 
    } 
    
    
    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), 
        NULL, ReplyBuffer, ReplySize, 1000); 

    free(ReplyBuffer); 

    IcmpCloseHandle(hIcmpFile); 

    if (dwRetVal == 0) 
        return GetLastError(); 
    
    return 0; 

} 

HB_FUNC( HB_PING ) 
{ 
   hb_retni( hb_Ping( hb_parc( 1 ) ) ); 
} 

#pragma ENDDUMP 

BPD
HMG Convert Dream into Reality

Post Reply