Internet connected or not
Posted: Sat Jun 17, 2017 9:35 am
Is there any reliable function to know computer is connected with Internet or not
Exclusive forum for HMG, a Free / Open Source xBase WIN32/64 Bits / GUI Development System
http://hmgforum.com/
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
Well, not exactly the "perfect" function
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
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
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