ccplusplus.com
Learn C, C++ Concepts
Wednesday, October 5, 2011
htons example in c
/*********************************************************************** * File : htons.c * Author : Saurabh Gupta * Desc : htons example * htons function in c * host to network short * 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 <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/un.h> #include <netinet/in.h> /* * This function reports the error and * exits back to the shell: */ static void displayError(const char *on_what) { perror(on_what); exit(1); } int main(int argc, char **argv, char **envp) { int z; /* Status return code */ int sck_inet; /* Socket */ struct sockaddr_in adr_inet;/* AF_INET */ int len_inet; /* length */ const unsigned char IPno[] = { 127, 0, 0, 23 /* Local loopback */ }; /* * Create an IPv4 Internet Socket */ sck_inet = socket(AF_INET,SOCK_STREAM,0); if ( sck_inet == -1 ) { displayError("socket()"); } /* * Create an AF_INET address */ memset(&adr_inet,0,sizeof adr_inet); adr_inet.sin_family = AF_INET; /* * see the usage of the htons function */ adr_inet.sin_port = htons(9000); memcpy (&adr_inet.sin_addr.s_addr,IPno,4); len_inet = sizeof adr_inet; /* * Now bind the address to the socket */ z = bind(sck_inet, (struct sockaddr *)&adr_inet, len_inet); if ( z == -1 ) { displayError("bind()"); } /* * Display all of our bound sockets */ system("netstat -pa --tcp 2>/dev/null | sed -n '1,/^Proto/p;/af_inet/p' "); close(sck_inet); return 0; } /* * OUTPUT * [sgupta@rhel54x64 htons example]$ gcc htons.c -o htons [sgupta@rhel54x64 htons example]$ ./htons Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name [sgupta@rhel54x64 htons example]$ */
See Also
htonl
ntohl
ntohs
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment