|
Home > Archive > Unix Programming > October 2005 > C-programming ip local machine
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
C-programming ip local machine
|
|
|
| I see that there are a topic almost like this, but I don't see anything
about C in that topic so here it goes:
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
| |
| Bill Marcum 2005-10-24, 3:48 pm |
| On Fri, 21 Oct 2005 07:52:04 +0200, Rusty
<andersru@stud.ntnu.no> wrote:
> I see that there are a topic almost like this, but I don't see anything
> about C in that topic so here it goes:
>
> 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:
>
A machine can have several ip's. You can look at the source code of
ifconfig to see how to find all of them.
--
Perl programming is an *empirical* science!
-- Larry Wall in <10226@jpl-devvax.JPL.NASA.GOV>
| |
| loic-dev@gmx.net 2005-10-24, 3:48 pm |
| Salut Rusty,
> I see that there are a topic almost like this, but I don't see anything
> about C in that topic so here it goes:
>
> 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:
The same question has been answered on c.u.p. a few days ago:
http://groups.google.com/group/comp...m/thread/36bdf=
53975611c99/
HTH,
Lo=EFc.
|
|
|
|
|