Ciao,
non riesco a capire una cosa.
Con questo programma:
#include <stdio.h>
#include <arpa/inet.h> // for inet_... functions
#include <sys/types.h> // for uint_t types
#include <netinet/in.h> // for ntoh / hton functions
#include "errlib.h"
int main (int argc, char *argv[])
{
struct in_addr x;
uint32_t num_addr;
if (argc != 2)
err_quit ("usage: %s address", argv[0]);
if (inet_aton (argv[1], &x) != 0)
{
printf("Senza ntohl: %lu\n", (unsigned long)x.s_addr );
/* ntohl converte il nostro indirizzo IP nel formato a 32 bit corretto!!! */
num_addr = ntohl (x.s_addr);
printf ("Con ntohl: %s = %lu\n", argv[1], (unsigned long)num_addr);
}
else
err_quit ("(%s) error - illegal address '%s'", argv[0], argv[1]);
return 0;
}
io ottengo in output:
$ ./in 192.168.1.1
Senza ntohl: 16885952
Con ntohl: 192.168.1.1 = 3232235777
$ ./in 1.1.1.1
Senza ntohl: 16843009
Con ntohl: 1.1.1.1 = 16843009
Perche' con 1.1.1.1 ottengo lo stesso risultato senza usare la funzione ntohl mentre con l'altro caso sono costretto ad usarlo.
Grazie