Connecting Tech Pros Worldwide Forums | Help | Site Map

Find ip on my local machine

Rusty
Guest
 
Posts: n/a
#1: Nov 15 '05
I am trying to find the ip of my machine, but all I get is the local
ip, not the ip I want. This is a program that finds the local ip, what
should be modified:

#include <stdio.h>
#include <netdb.h>

int main()
{
char hostn[400]; //placeholder for the hostname
struct hostent *hostIP; //placeholder for the IP address

//if the gethostname returns a name then the program will get the ip
address using gethostbyname
if((gethostname(hostn, sizeof(hostn))) == 0)
{
hostIP = gethostbyname(hostn); //the netdb.h function gethostbyname
printf("IP address: %s\n", inet_ntoa(*(struct in_addr
*)hostIP->h_addr));
}
else
{
printf("ERROR:FC4539 - IP Address not found."); //error if the
hostname is not found
}
return 0;
}

Rusty
Norway


Keith Thompson
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Find ip on my local machine


"Rusty" <andersrustad@yahoo.no> writes:[color=blue]
> I am trying to find the ip of my machine, but all I get is the local
> ip, not the ip I want. This is a program that finds the local ip, what
> should be modified:[/color]

You can't do that in standard C, which has no support for networking.
[color=blue]
> #include <stdio.h>
> #include <netdb.h>[/color]

The <netdb.h> header is not defined by the C standard.

You'll need to ask in a newsgroup specific to your system, perhaps
comp.unix.programming.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Closed Thread