FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Function IsInternet()
Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Function IsInternet()

Posted: Tue Jun 25, 2013 06:40 PM
Para los maestros: Cual és la mejor FUNCTION?

Esta?

Code (fw): Select all Collapse
FUNCTION IsInternet()

   LOCAL cIp, cVret := .F.

   WsaStartUp()

   // conforme pessoal do forum, e para chamar duas vezes ??
   cIp := GETHOSTBYNAME( "microsoft.com" )
   cIp := GETHOSTBYNAME( "microsoft.com" )

   WsaCleanUp()

   // Seguranca caso o provedor da microsoft esteja fora do ar...
   IF ( cIp <= "0.0.0.0" )

      WsaStartUp()

      cIp := GetHostByName( "google.com" )
      cIp := GetHostByName( "google.com" )

      WsaCleanUp()

   ENDIF

RETURN( cIp<>"0.0.0.0" )

Ó Esta?

FUNCTION IsInternet()

   LOCAL pSocket, lOk := .F.

   INetInit()

   pSocket := INetConnect( "www.microsoft.com", 80 )

   //? INetAddress( pSocket )

   IF INetAddress( pSocket ) != "0"
      lOk := .T.
   ELSE
      pSocket := INetConnect( "www.hotmail.com", 80 )
      IF INetAddress( pSocket ) != "0"
         lOk := .T.
      ENDIF
   ENDIF

   INetClose( pSocket )
   INetCleanUp()

RETURN( lOk )


Gracias, salu2
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM

Re: Function IsInternet()

Posted: Thu Jun 27, 2013 04:26 PM

Podrías hacer una función que haga un PING a google por ejemplo...
Yo hace mucho tiempo había tirado una función aquí en el foro... Ahora debería buscarla. Pero seguramente aquí en el foro debería estar.

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM

Re: Function IsInternet()

Posted: Thu Jun 27, 2013 04:32 PM

Hola...
Aquí el link de aquella función que te mencionaba: viewtopic.php?f=6&t=20679&p=109714&hilit=ping#p109714

Espero te sirva la idea.
Saludos,

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 12:04 PM

Gracias, voy probar.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 44508
Joined: Thu Oct 06, 2005 05:47 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 12:28 PM

El código de Eduardo para hacer ping es muy interesante para saber hacer ping, pero desde su propio código se llama a GetHostByName(), que ya implica que hay internet (salgo que tomase el resultado de un DNS en cache, no se si esto se pueda dar).

Lo que quiero decir es que si funciona GetHostByName() eso ya significa que hay internet :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 12:43 PM
Gracias maestro Antonio, mi duda és cual é la diferença entre la dos functions?

Esta é mejor que la function standard de fivewin?

Code (fw): Select all Collapse
FUNCTION IsInternet()
 
   LOCAL pSocket, lOk := .F.
 
   INetInit()
 
   pSocket := INetConnect( "www.microsoft.com", 80 )
 
   //? INetAddress( pSocket )
 
   IF INetAddress( pSocket ) != "0"
      lOk := .T.
   ELSE
      pSocket := INetConnect( "www.hotmail.com", 80 )
      IF INetAddress( pSocket ) != "0"
         lOk := .T.
      ENDIF
   ENDIF
 
   INetClose( pSocket )
   INetCleanUp()
 
RETURN( lOk )


Salu2
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1713
Joined: Tue Oct 28, 2008 06:26 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 01:42 PM
Karina

En tu ejemplo siempre el pSocket obtiene una IP tenga o no Internet.
Y varia cuando se sale por un PROXY en las empresas la mayoría usa.
Mejor el buscar un dominio cercano con IP estable para comparar

Modifique el código a lo de mi empresa y funciona de maravilla.
Code (fw): Select all Collapse
#include "FiveWin.ch"

FUNCTION Main()
  Private lOk:=.F.
  If IsInternet()
    ?"Si"
  Else
     ?"No"
  Endif
Return

FUNCTION IsInternet()
   LOCAL pSocket

   INetInit()

   pSocket := INetConnect( "www.lostajiboshotel.com", 80 )

   IF INetAddress( pSocket )="192.10.1.9" .or. INetAddress( pSocket )="200.58.169.10" //EXCHANGE CON PROXY Y DOMINIO SIN PROXY
       lOk := .T.
   ENDIF

   INetClose( pSocket )
   INetCleanUp()

RETURN( lOk )

El código es mucho mas corto y efectivo.

Saludos,

Adhemar

Saludos,



Adhemar Cuéllar

Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 02:22 PM

Gracias Adhemar, talvez és por eso que las veces la función IsInternet() devuelve que no tiene internet para la IP de la computadora se és fijado?

Gracias,

Salu2

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1530
Joined: Thu Oct 30, 2008 02:37 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 02:36 PM

Adhemar,

Ando cortito en asuntos web.
¿ Puede explicar la funcion ? ¿ es válida para cualquier computador ?
¿ que hacen las distintas funciones Inet ? ¿ Por qué se pregunta por unas IP concretas ?

Gracias.

Posts: 1713
Joined: Tue Oct 28, 2008 06:26 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 02:50 PM

hmpaquito

Se pregunta por una IP concreta porque si tu conexión sale por PROXI la IP de www.microsoft.com es diferente a si salis directamente (No se porque)
Ej. con PROXY 108.83.52.0 y sin 64.4.11.42 y es mas lento
PROXI es un servidor de navegación (ISA o TMG) el cual tiene todos los filtros (Firewall) de seguridad generalmente usado en empresas.

Tendríamos que buscar una IP que no varie en mi caso yo uso en dominio de mi empresa.

Saludos,

Adhemar

Saludos,



Adhemar Cuéllar

Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 03:01 PM
Adhemar, porque no funciona con www.microsoft.com ??

Code (fw): Select all Collapse
#include "fivewin.ch"

// PRUEBA DE PING //

function main()

   if WSAStartup() != 0
      MsgAlert( "WSAStartup error" )
      return nil
   endif
     
   /* // Funciona perfecto
   if hb_Ping( GetHostByName( "www.google.com" ) ) == 0
      Msginfo("Respuesta correcta del host " + AllTrim( GetHostByName( "www.google.com" ) ), "Atención" )
   else
      Msginfo("Host inaccesible" + AllTrim( GetHostByName( "www.google.com" ) ), "Atención" )
   endif
  
   Ping( GetHostByName( "www.google.com" ) )
   */


   /* // funciona perfecto
   if hb_Ping( GetHostByName( "www.lostajiboshotel.com" ) ) == 0
      Msginfo("Respuesta correcta del host " + AllTrim( GetHostByName( "www.lostajiboshotel.com" ) ), "Atención" )
   else
      Msginfo("Host inaccesible " + AllTrim( GetHostByName( "www.lostajiboshotel.com" ) ), "Atención" )
   endif
  
   Ping( GetHostByName( "www.lostajiboshotel.com" ) )
   */

   // Funciona perfecto
   */
   if hb_Ping( GetHostByName( "www.hotmail.com" ) ) == 0
      Msginfo("Respuesta correcta del host " + AllTrim( GetHostByName( "www.hotmail.com" ) ), "Atención" )
   else
      Msginfo("Host inaccesible" + AllTrim( GetHostByName( "www.hotmail.com" ) ), "Atención" )
   endif
  
   Ping( GetHostByName( "www.hotmail.com" ) )
   */

   // Adhemar, porque no funciona con <!-- w --><a class="postlink" href="http://www.microsoft">www.microsoft</a><!-- w --> ??

   if hb_Ping( GetHostByName( "www.microsoft.com" ) ) == 0
      Msginfo("Respuesta correcta del host " + AllTrim( GetHostByName( "www.microsoft.com" ) ), "Atención" )
   else
      Msginfo("Host inaccesible " + AllTrim( GetHostByName( "www.microsoft.com" ) ), "Atención" )
   endif
  
   Ping( GetHostByName( "www.microsoft.com" ) )
   
   WSACleanUp()
   
return nil

Function Ping(DestinationAddress)
Local IcmpHandle,Replicas,puerto
Local RequestData   :="Probando ping",;
      RequestSize   :=15,;
      RequestOptions:="",;
      ReplyBuffer   :=SPACE(278),;
      ReplySize     :=278,;
      Timeout       := 1000 && Milisegundos de espera

DEFAULT DestinationAddress := "127.0.0.1"

DestinationAddress:=LEFT(ALLTRIM(DestinationAddress)+SPACE(15),15)

MsgGet("Ping...","Ingrese una dirección IP",@DestinationAddress)

IcmpHandle:=IcmpCreateFile()

Replicas  :=IcmpSendEcho(IcmpHandle,;
                          inet_addr(DestinationAddress),;
                          RequestData,;
                          RequestSize,0,;
                          ReplyBuffer,;
                          ReplySize,;
                          Timeout)

IcmpCloseHandle(IcmpHandle)

IF Replicas > 0
   Msginfo("Respuesta correcta del host "+ALLTRIM(DestinationAddress),"Atención")
ELSE
   Msginfo("Host inaccesible"+ALLTRIM(DestinationAddress)+"Atención")
ENDIF

Return nil
//

DLL32 FUNCTION WSAGetLastError() AS _INT PASCAL FROM "WSAGetLastError" LIB "wsock32.dll"
DLL32 FUNCTION inet_addr(cIP AS STRING) AS LONG PASCAL FROM "inet_addr" LIB "wsock32.dll"
DLL32 FUNCTION IcmpCreateFile() AS LONG PASCAL FROM "IcmpCreateFile" LIB "icmp.dll"
DLL32 FUNCTION IcmpCloseHandle(IcmpHandle AS LONG) AS LONG PASCAL FROM "IcmpCloseHandle" LIB "icmp.dll"
DLL32 FUNCTION IcmpSendEcho(IcmpHandle AS LONG,;
                            DestinationAddress AS LONG,;
                            RequestData AS STRING,;
                            RequestSize AS LONG,;
                            RequestOptions AS LONG,;
                            ReplyBuffer AS LPSTR,;
                            ReplySize AS LONG,;
                            Timeout AS LONG) AS LONG PASCAL FROM "IcmpSendEcho" LIB "icmp.dll"

#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;
    DWORD dwRetVal;
    char SendData[32] = "Data Buffer";
    LPVOID ReplyBuffer;
    DWORD ReplySize;

    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)
        return 3;
    
    
    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), 
        NULL, ReplyBuffer, ReplySize, 1000);

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

}


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

#pragma ENDDUMP


Gracias,
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 601
Joined: Wed Jul 04, 2007 03:51 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 03:15 PM

Hay muchos sitios web que optan por bloquear la respuesta a los PINGs.
El sitio www.microsoft.com es un caso concreto de esos. El motivo por el cual optan por no responder pueden ser varios, seguridad, etc...

Ojeda Esteban Eduardo.

Buenos Aires - Argentina.

FWH - PellesC - DBF/CDX - ADS - Gloriosos .Bat - MySql - C# .net - FastReport

Skype: jreduojeda
Posts: 1713
Joined: Tue Oct 28, 2008 06:26 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 03:21 PM

He intentado con www.hotmail.com y www.google.com y también varian me parece que es así por seguridad.
Deben tener algún software que las hace variar.

Saludos,

Adhemar

Saludos,



Adhemar Cuéllar

Posts: 8716
Joined: Tue Dec 20, 2005 07:36 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 03:42 PM

Gracias,

Adhemar puedo usar tú sitio para hacer la conexion con la internet?

Salu2

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1713
Joined: Tue Oct 28, 2008 06:26 PM

Re: Function IsInternet()

Posted: Fri Jun 28, 2013 03:49 PM

Claro, no hay problema.

Saludos,

Adhemar

Saludos,



Adhemar Cuéllar

Continue the discussion