Chapter Contents

Previous

Next
gethostid

gethostid



Gets the Local Host's Internet Address

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <netdb.h>

unsigned long gethostid(void);


DESCRIPTION

gethostid gets the 32-bit Internet address for the local host.


RETURN VALUE

gethostid returns the Internet address or -1 ( 0xFFFFFFFF ), which is the value of the macro identifier INADDR_NONE in the <netinet/in.h> header file.


PORTABILITY

gethostid calls are not necessarily portable to all systems. In particular, the return value may not be the IP address. However, gethostid is portable to many other environments, including most UNIX systems, that implement BSD sockets. These other socket library implementations do not require the inclusion of the <netdb.h> header file. The SAS/C <netdb.h> header file contains #pragma map statements to create unique eight-character identifiers for the OS/390 and CMS linking utilities. To reduce incompatibilities caused by failure to include <netdb.h> in existing source code, a #pragma map statement for this function is also available in <sys/types.h> .


IMPLEMENTATION

The value that gethostid returns is assigned by the local host's TCP/IP software, which must be running in order for gethostid to succeed.


EXAMPLE

This program uses the socket call, gethostid to return the 32-bit internet address for the local host. The local host must have an operational TCPIP stack.

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

main()
{
   struct in_addr in;

/* gethostid() returns the 32-bit internet address as an    */
/* unsigned long.                                           */
   in.s_addr = gethostid();
   if (in.s_addr == INADDR_NONE) {
      perror("gethostid failed");
      return EXIT_FAILURE;
   }

/* convert the unsigned long to a string in dotted decimal  */
/* and print.                                               */

   printf("Local Host IP Address is: %s\n",inet_ntoa(in));
   return EXIT_SUCCESS;
}


RELATED FUNCTIONS

gethostname


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.