ccplusplus.com
Learn C, C++ Concepts
Wednesday, October 5, 2011
htonl example c
/*********************************************************************** * File : htonl.c * Author : Saurabh Gupta * Desc : htonl example * : socket htonl * : host to network byte order * Source : http://saurabhgupta0527.blogspot.com/2011/07/socket.html * Created : AM 06:06 05 October 2011 * Note : ************************************************************************/ #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("%4s = 0x%08X net 0x%08X\n", addr[x],net_addr, (unsigned long)htonl(net_addr)); } return 0; } /* * OUTPUT * [sgupta@rhel55x86 htonl example]$ gcc htonl.c -o htonl [sgupta@rhel55x86 htonl example]$ ./htonl 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 htonl example]$ */
See Also
htons
ntohl
ntohs
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment