Find ip on my local machine

R

Rusty

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
 
M

Michael Wojcik

I am trying to find the ip of my machine, but all I get is the local
ip, not the ip I want.

Off-topic for comp.lang.c, where we discuss the standard C language,
which knows nothing of IP addresses or anything else about networking.

I recommend you try a group specific to your platform. If you're
working on a Unix system, for example, try comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top