Sapere se un indirizzo web è valido

Da Gambas-it.org - Wikipedia.

Per sapere se una URL è valida, e quindi esistente, si può utilizzare la funzione esterna "gethostbyname()" contenuta dalla libreria condivisa: "libc.so.6 ".

Mostriamo un semplice esempio:

Library "libc:6"

Public Struct hostent
  h_name As Pointer
  h_aliases As Pointer
  h_addrtype As Integer
  h_length As Integer
  h_addr_list As Pointer
End Struct

' struct hostent *gethostbyname(const char *name)'
' Returns a structure of type hostent for the given host name.
Private Extern gethostbyname(idn As String) As Hostent


Public Sub Main()
 
 Dim host As Hostent
 Dim idn As String

' Impostiamo il nome di un host esistente, quindi valido:
 idn = "www.gambas-it.org"
  
 host = gethostbyname(idn)

 Print "Ping: host "; Quote(idn); " valido\n"
 Print "Nome ufficiale dell'host: "; String@(host.h_name)
 Print "Nome alias dell'host:     "; String@(Pointer@(host.h_aliases))
 Print "Indirizzo IP dell'host:   "; String@(Pointer@(host.h_addr_list) + 32)

End

Se l'host è inesistente, sarà sollevato un errore di segmentazione (11).