ccplusplus.com
Learn C, C++ Concepts
Wednesday, September 7, 2011
inet_netof example in c
/* inet_netof.c * inet_netof example c code * inet_netof sample c code * Demonstrate inet_netof functions; */ #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv){ int x; struct sockaddr_in adr_inet;/* AF_INET */ const char *addr[] = { "44.135.86.12", "127.0.0.1", "172.16.23.95", "192.168.9.1" }; unsigned long net, hst; for ( x=0; x<4; ++x ) { /* * Create a socket address: */ memset(&adr_inet,0,sizeof adr_inet); adr_inet.sin_family = AF_INET; adr_inet.sin_port = htons(9000); if ( !inet_aton(addr[x], &adr_inet.sin_addr) ) { puts("bad address."); } /* * Split address into Host & Net ID */ net = inet_netof(adr_inet.sin_addr); printf("%14s : net=0x%08lX host=0x%08lX\n", inet_ntoa(adr_inet.sin_addr),net,hst); /* * Zero the address to prove later that * we can reconstruct this value: */ memset(&adr_inet,0,sizeof adr_inet); adr_inet.sin_family = AF_INET; adr_inet.sin_port = htons(9000); adr_inet.sin_addr = inet_makeaddr(net,hst); /* * Now display the reconstructed * address: */ printf("%14s : %s\n\n", "inet_makeaddr", inet_ntoa(adr_inet.sin_addr)); } } /* * * [sgupta@rhel54x64 socket]$ gcc inet_netof.c -o inet_netof [sgupta@rhel54x64 socket]$ ./inet_netof 44.135.86.12 : net=0x0000002C host=0x00000000 inet_makeaddr : 44.0.0.0 127.0.0.1 : net=0x0000007F host=0x00000000 inet_makeaddr : 127.0.0.0 72.16.23.95 : net=0x0000AC10 host=0x00000000 inet_makeaddr : 172.16.0.0 192.168.9.1 : net=0x00C0A809 host=0x00000000 inet_makeaddr : 192.168.9.0 [sgupta@rhel54x64 socket]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment