How to get IP Address of machine?

The best solution to stuff like this is to browse some source code. There is a ton of source code on the internet. And there are several programs that format ip addresses. But I'm trapped at the office waiting for a tech, and I don't have anything better to do, so...

#ifdef __STDC__
#define PROTOTYPICAL
#endif
#ifdef __cplusplus
#define PROTOTYPICAL
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>


#ifdef PROTOTYPICAL
int main(int argc, char *argv[])
#else
main(argc,argv)
char *argv[];
#endif
{
	char *mess;
	struct hostent *hp;
	int dumpall;

	dumpall=0;
	while(*++argv) {
		if(!strcmp(*argv,(char *)"-d")) {
			dumpall=!dumpall;
			continue;
		}
		/*
		 *  Call gethostbyname for current argument
		 */
		if(! (hp = gethostbyname(*argv))) {
			switch(h_errno){
					case HOST_NOT_FOUND:  
						mess=(char *)"Not Found";
						break;
					case TRY_AGAIN:  
						mess=(char *)"Time Out";
						break;
					case NO_RECOVERY:  
						mess=(char *)"No Recovery";
						break;
					case NO_ADDRESS:  
						mess=(char *)"No Address";
						break;
					default: 
						mess=(char *)"unknown error";
						break;
			}
			printf("%s  %s\n", *argv, mess);

		} else {
			printf("%s  %s  %s \n", *argv, hp->h_name,
				inet_ntoa(*(struct in_addr *)(hp->h_addr)));
			if(dumpall) {
				printf("  addresses:\n");
				while(*(hp->h_addr_list)){
					printf("      %s \n", 
						inet_ntoa(*(struct in_addr *)*(hp->h_addr_list)++));
				}
				printf("  aliases:\n");
				while(*(hp->h_aliases)){
					printf("      %s \n",*(hp->h_aliases)++);
				} 
			}
		}
	}
	exit(0);
}

This should work with any c or c++ compiler. But I only tested on HP-UX.

Yow!! We have 4 hour response time.

You cannot do that portably and it usually requires root power. Look at the source code for lsof. It does stuff like that.

But all sockets structures store ip address not domain names