Tuesday, August 2, 2011

inet_network sample C code

/* network.c:
 *
 * Example using inet_network(3):
 */
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>

 int  main(int argc,char **argv) {
    int x;
    const char *addr[] = {
       "44.135.86.12",
       "127.0.0.1",
       "172.16.23.95",
       "192.168.9.1"
    };
    unsigned long net_addr;

    for ( x=0; x<4; ++x ) {
       net_addr = inet_network(addr[x]);
       // printf("%14s = 0x%081X net 0x%081X\n",     addr[x],net_addr,(unsigned long)htonl(net_addr));
       printf("%4s = 0x%08X net 0x%08X\n", addr[x],net_addr,
       (unsigned long)htonl(net_addr));
    }
    return 0;
 }
/*
 * OUTPUT
 *
 [sgupta@rhel55x86 Linux-Socket-Programming]$ ./inet_network
 44.135.86.12    =       0x2C87560C net 0x0C56872C
 127.0.0.1       =       0x7F000001 net 0x0100007F
 172.16.23.95    =       0xAC10175F net 0x5F1710AC
 192.168.9.1     =       0xC0A80901 net 0x0109A8C0
 [sgupta@rhel55x86 Linux-Socket-Programming]$
 */

No comments:

Post a Comment