ccplusplus.com
Learn C, C++ Concepts
Thursday, November 3, 2011
gethostname example c
/*********************************************************************** * File : gethostname.c * Author : Saurabh Gupta * Desc : Demonstrate gethostname(2) * gethostname example * gethostname example c * gethostname example linux * gethostname function linux * Source : http://saurabhgupta0527.blogspot.com/2011/07/socket.html * Created : AM 10:27 22 August 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> #include <arpa/inet.h> int main(int argc,char **argv) { int z; char buf[32]; z = gethostname(buf,sizeof buf); if ( z == -1 ) { fprintf(stderr, "%s: gethostname(2)\n", strerror(errno)); exit(1); } printf("host name = '%s'\n",buf); z = getdomainname(buf,sizeof buf); if ( z == -1 ) { fprintf(stderr,"%s: getdomainname(2)\n", strerror(errno)); exit(1); } printf("domain name = '%s'\n",buf); return 0; } /* * OUTPUT * [sgupta@rhel55x86 socket]$ gcc gethostname.c -o gethostname [sgupta@rhel55x86 socket]$ ./gethostname host name = 'rhel55x86.ind.com' domain name = 'ind.com' [sgupta@rhel55x86 socket]$ */
See Also:
IPC Socket Concepts and Example Codes
IPC Advance Socket Concepts and Example Codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment