FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Function IsInternet()
Posts: 8515
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: 8515
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: 44158
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: 8515
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: 1710
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 C.
Posts: 8515
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: 1515
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: 1710
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 C.
Posts: 8515
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: 1710
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 C.
Posts: 8515
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: 1710
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 C.

Continue the discussion