Tengo coneccion a Internet ? / I have internet connexion ?

Moderator: Rathinagiri

Post Reply
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Tengo coneccion a Internet ? / I have internet connexion ?

Post by andyglezl »

Hola

Revisando este post en donde Marek comparte esta funcion, cree esta variante.
En cierto momento queremos saber si hay conexion a Internet.
Espero les sirva.
---------------------------------------------------------------------------
Hello

Reviewing this post where Marek shares this function, create this variant.
At some point we want to know if there is internet connection.
I hope they serve.

http://hmgforum.com/viewtopic.php?f=12& ... ate#p28507


Code: Select all

* MsgBox( IF( HayInternet( ), "INTERNET:   Connected ...", "INTERNET:   Not Connected..." ) )

FUNCTION HayInternet( )
	LOCAL oCon, oUrl, lRet := .f.
	LOCAL oOldErrorHandle, cUrl, cLocalFileName
   
	cUrl := "http://www.microsoft.com/es-mx/default.aspx"
	cLocalFileName := System.TempFolder+"InternetOK.txt"
	IF FILE( cLocalFileName )
		Ferase( cLocalFileName )
	ENDIF
	oOldErrorHandle:= ErrorBlock({|e| break(e)})
   
	BEGIN SEQUENCE
	oUrl := tURL():New( cUrl )
	oCon := TipClientHttp():New( oUrl )
	oCon:nConnTimeout := 20000
	IF oCon:Open( cUrl )
		oCon:WriteAll( cLocalFileName )
		oCon:Close()
		IF FILE( cLocalFileName )
			lRet := .T.
			*MsgBox( "Connected..." )
		ELSE
			lRet := .F.
		ENDIF
	ELSE
		lRet := .f.
	END
	RECOVER
		*MSGSTOP("Connection with server couldn't be completed")
	END SEQUENCE
	ErrorBlock(oOldErrorHandle)
RETURN( lRet )
Andrés González López
Desde Guadalajara, Jalisco. México.
Javier Tovar
Posts: 1275
Joined: Tue Sep 03, 2013 4:22 am
Location: Tecámac, México

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by Javier Tovar »

Hola Andres,

Revisando tu función, al tener yo internet en la PC, si me dice que hay internet, si le vuelvo a preguntar si hay internet, también me responde acertadamente. Pero si desconecto el cable y ejecuto el programa me dice que NO hay internet lo cual esta bien, pero si le vuelvo a preguntar me dice que SI y es un error.

No se si a los demás le pasa lo mismo?

Saludos y gracias por compartir.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Hi Andres,

Reviewing your function, I have internet on PC, if internet tells me there if I ask again if there is internet, I also answered correctly. But if I disconnect the cable and run the program tells me there is NO internet which is fine, but if you ask again and tells me that SI is a mistake.

No others if you feel the same?

Greetings and thanks for sharing.
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by danielmaximiliano »

es necesario hacer un Refresh a la consulta.

en Harbour se encuentra este ejemplo que utiliza WinInet y la funcion

Code: Select all

/*
 * Harbour Project source code:
 *
 * Copyright 2010 Viktor Szakats (vszakats.net/harbour)
 * www - http://harbour-project.org
 *
 */

#require "hbmisc"

PROCEDURE Main()

#if defined( __PLATFORM__WINDOWS )

   LOCAL nFlags

   nFlags := -1
   ? CallDll32( "InternetGetConnectedState", "wininet.dll", @nFlags, 0 )
   ? nFlags

#endif

   RETURN
Minimum supported client : Windows 2000 Professional
Minimum supported server : Windows 2000 Server

Header : Wininet.h
Library : Wininet.lib
DLL [ Wininet.dll
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by andyglezl »

... pero si le vuelvo a preguntar me dice que SI y es un error.
Hola Javier, pudieran ser cuestion de permisos...
Has la prueba quitando:
----------------------------------------------
Hi Javier, could be a matter of permissions ...
You try removing the:

cLocalFileName := System.TempFolder+"InternetOK.txt"
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by zolysoftsolutions »

Hi,
As I observ too, second time check the code return ok for internet connection.
The problem is that at first check the code try to write the textfile..but at second check, when is no internet connection too, the local file is remaining opened (from the first check), and cannot be deleted at begin of the function. Probably because the file is not closed after write with an empty string..or just waiting for the website to answer..I don't know.
Is not a matter of rights, for sure. With internet connection I can delete the file, when is no connection the explorer answer that the file is in use and cannot be deleted.
The code will return ok for connection because the file still exists .
Can somebody tell tu us how to delete the file when the "writeall" don't stop together with Close()

Zoli B.
_______________________________
Open eyes for you!
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by andyglezl »

Hola Zoli B.

Puedes modificar el url por cualquier otro y descargar tan solo un archivo con una letra.
Yo puse esta direccion pensando que es dificil que el servidor de Microsoft este caido
y falle la funcion.
Este archivo "default.aspx" pesa 94 Kb y se me hace raro que tarde mucho en bajar.
---------------------------------------------------------------------------------
Hi Zoli B.

You can modify the url and download any other file with only a single letter.
I put this address thinking it is difficult for the Microsoft server this Fallen
and the function fails.
This file "default.aspx" and weighs 94 Kb makes me weird taking a while to download.


cUrl := "http://www.microsoft.com/es-mx/default.aspx"
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by zolysoftsolutions »

Hello Andy.

I also tried to change. Still is the same.
The problem is that when is no connection the file from the http cannot be read an the code still try to writeall to the local file..here the process is blocked.. when i acces second time the funtion the file is still open (trying to write) an is NOT deleted ant the code return .t. for lret.

Best regards,
Zoli
_______________________________
Open eyes for you!
User avatar
danielmaximiliano
Posts: 2612
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by danielmaximiliano »

Andy :
El metodo :Open() de la libreria HBTip dentro de un Begin/End es suficiente si devuelve lRet como estado para saber si una direccion responde; creo que la escritura de un archivo y preguntar por el es un paso de más.
creo que puedes usar :GetOk que tambien tiene el estado de exito de :Open()

Andy:
The method :Open() in the library HBTip within a Begin/End is sufficient if lRet as state returns to see if an address responds, I think writing a file and ask for is a step too far.
I think you can use :GetOk() that also has success status :Open()

Code: Select all

    IF oCon:Open( cUrl )
              oCon:Close()
             ELSE
     END
   RECOVER
      *MSGSTOP("Connection with server couldn't be completed")
   END SEQUENCE
   ErrorBlock(oOldErrorHandle)
RETURN( oCon:GetOk() )
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Tengo coneccion a Internet ? / I have internet connexi

Post by andyglezl »

Hola Max y demas

Yo probé lo que me indicas y con :Open( cUrl ) siempre me regresa .T.,
y :GetOk() no me regresa nada.

Vuelvo a poner oCon:WriteAll( cLocalFileName ) y funciona.
aclaro que en mi caso solo llamo la funcion al entrar al programa y al salir
libera el archivo que grabó y ya lo puedo borrar.

Pero si, definitivamente da problemas cuando quitas la coneccion a internet
y ejecutas la funcion, se queda en un ciclo y no permite borrar el archivo.
-------------------------------------------------------------------------
Hi Max and other

I tried what you state and :Open(cUrl) always returns .T. ,
and :GetOk () does not return me anything.

I restart oCon:WriteAll(cLocalFileName) and it works.
clarify that in my case just call the function when entering, and exiting the program
free file already recorded and I can delete.

But yeah, definitely gives problems when you remove the internet connection
and execute the function, remains in a cycle and not delete the file.
Andrés González López
Desde Guadalajara, Jalisco. México.
Post Reply