PDA

View Full Version : [C/C++] Host>IP


Fede_Reghe
27-03-2007, 20:43
Come faccio a ottenere da un host un ip? gethostbyaddr?

andbin
27-03-2007, 20:51
Come faccio a ottenere da un host un ip? gethostbyaddr?Se parti da un hostname, gethostbyname()

Fede_Reghe
27-03-2007, 20:54
Perchè da che parti con gethostbyaddr?

Cmq come glieli devo passare i valori? perchè se metto "ciao.it" mi dice: "cannot convert `hostent*' to `const char*' for argument `1' to `long unsigned int inet_addr(const char*)' "

andbin
27-03-2007, 21:16
Perchè da che parti con gethostbyaddr?

Cmq come glieli devo passare i valori? perchè se metto "ciao.it" mi dice: "cannot convert `hostent*' to `const char*' for argument `1' to `long unsigned int inet_addr(const char*)' "#include <stdio.h>
#include <netdb.h>

int main (void)
{
struct hostent *he;

he = gethostbyname ("www.hwupgrade.it");

if (he != NULL)
{
int i;
for (i=0; i<he->h_length; i++)
printf (i > 0 ? ".%d" : "%d", he->h_addr_list[0][i] & 255);

printf ("\n");
}

return 0;
}
Testato su Linux, stampa il primo IP (ce ne possono essere di più infatti).
Su Windows sicuramente c'è da cambiare l'include.