ccplusplus.com
Learn C, C++ Concepts
Monday, November 14, 2011
inet_ntoa example in c
<br /> /*************************************************************<br /> * File : inet_ntoa.c<br /> * Author : Saurabh Gupta<br /> * Desc : Example using inet_ntoa<br /> * Source : http://saurabhgupta0527.blogspot.com/2011/07/socket.html<br /> * Created : AM 09:15 14 November 2011<br /> ************************************************************/<br /> #include <stdio.h><br /> #include <unistd.h><br /> #include <stdlib.h><br /> #include <sys/types.h><br /> #include <sys/socket.h><br /> #include <netinet/in.h><br /> #include <arpa/inet.h><br /> <br /> int<br /> main(int argc,char **argv) {<br /> struct sockaddr_in adr_inet;/* AF_INET */<br /> int len_inet; /* length */<br /> <br /> /*<br /> * Establish address (pretend we got<br /> * this address from a connecting<br /> * client):<br /> */<br /> memset(&adr_inet,0,sizeof adr_inet);<br /> <br /> adr_inet.sin_family = AF_INET;<br /> adr_inet.sin_port = htons(9000);<br /> <br /> if ( !inet_aton("127.0.0.23",<br /> &adr_inet.sin_addr) )<br /> puts("bad address.");<br /> <br /> len_inet = sizeof adr_inet;<br /> <br /> /*<br /> * Demonstrate use of inet_ntoa(3):<br /> */<br /> printf("The IP Address is %s\n",<br /> inet_ntoa(adr_inet.sin_addr));<br /> <br /> return 0;<br /> }<br /> /*<br /> * OUTPUT<br /> *<br /> [sgupta@rhel55x86 socket]$ gcc inet_ntoa.c -o inet_ntoa<br /> [sgupta@rhel55x86 socket]$ ./inet_ntoa<br /> The IP Address is 127.0.0.23<br /> [sgupta@rhel55x86 socket]$ <br /> */<br />
See Also:
inet_addr
,
inet_network
,
inet_lnaof
,
inet_makeddr
,
inet_netof
Socket Tutorial and sample codes
Advance Socket Concept Tutorial and sample codes
Advance C++ Concepts and sample codes
1 comment:
Anonymous
December 27, 2011 at 6:48 PM
nice collection.
thanks for keeping all these socket codes in one place.
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
nice collection.
ReplyDeletethanks for keeping all these socket codes in one place.