Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    23

    [C] Indirizzo IP di un host

    Salve! Qualcuno sa dirmi come posso trovare l'indirizzo IP di un host?
    Ad esempio come trovo l'IP di "forum.html.it"?

    ps: Uso Windows XP

    Grazie!

  2. #2
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    Per scrivere un programma in C?

    Si usa la funzione gethostbyname, di cui puoi trovare la documentazione sul sito della msnd.
    Per vedere l'IP di un sito che hai visitato puoi usare il comando

    ipconfig /displaydns

    Se l'opzione displaydns è supportata.

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    23
    Si in C!

    Non riesco ad usare la funzione gethostbyname per ottenere l'IP!

  4. #4
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    Questa funzione resituisce una struttura hostent che, tra le altre cose, contiene un array di indirizzi associati a quel nome (un array di tipo char **).
    Prendi un indirizzo di quelli e fagli seguire la solita trafila.
    Ciao!

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    23
    Ho già provato con:

    ...
    hostent *HOST;
    HOST=gethostbyname("www.google.it");

    printf("%s\n",HOST->h_name);

    for(int x=0;HOST->h_aliases[x]!=NULL;x++)
    printf("%s\n",HOST->h_aliases[x]);

    for(int x=0;HOST->h_addr_list[x]!=NULL;x++)
    printf("%s\n",HOST->h_addr_list[x]);
    ...

    ma non riesco ad ottenere l'IP.

  6. #6
    Utente di HTML.it L'avatar di ibykos
    Registrato dal
    Feb 2005
    Messaggi
    201
    Guarda questo sorgente scritto per Cygwin (tralasciando le funzioni non disponibili)

    codice:
    //
    // File: info_from_n.c
    // Purpose:
    // - retrieve all info about a host, given its name
    // - demonstrate use of gethostbyname()
    //
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <string.h>
    #include <netdb.h>
    
    #include "errlib.h"
    
    char *prog;
    
    int main (int argc, char *argv[])
    {
      struct hostent *hptr;
      char **pptr;
    
      // for libraries to know the program name
    
      prog = argv[0];
    
      // check the arguments
    
      if (argc != 2)
        err_quit ("usage: %s hostname", prog);
    
      // retrieve host info
    
      if ((hptr = gethostbyname(argv[1])) == NULL)
        err_quit ("(%s) error - gethostbyname() failed for '%s' : %s",
          prog, argv[1], hstrerror(h_errno));
    
      // output host info
    
      printf ("Information available about node '%s':\n", argv[1]);
    
      printf ("- canonical name = '%s'\n", hptr->h_name);
    
      if (hptr->h_aliases[0] == NULL)
        printf ("- aliases = (none)\n");
      else
        for (pptr = hptr->h_aliases; *pptr!=NULL; pptr++)
          printf ("- alias = '%s'\n", *pptr);
    
      printf ("- address type = ");
      if (hptr->h_addrtype == AF_INET)
        puts ("IPv4");
    //else if (hptr->h_addrtype == AF_INET6)
    //  puts ("IPv6");
      else
        puts ("(unknown)");
    
      if (hptr->h_addr_list[0] == NULL)
        printf ("- addresses = (none)\n");
      else {
        for (pptr = hptr->h_addr_list; *pptr!=NULL; pptr++) {
          printf ("- address = %s\n", inet_ntoa(**((struct in_addr**)pptr)));
        }
      }
    
      // done
    
      return 0;
    }

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2005
    Messaggi
    23
    OK! Ora funziona!!

    Grazie 1000! ibykos

    :maLOL:

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.