Getting IP address from C program.

Hi.

Is there a way to get the machine IP address from a C program?

Thanks

Augie

In article <8vdsrd$1n0$1@inn.qnx.com>, augiehenriques@hotmail.com says…

Hi.

Is there a way to get the machine IP address from a C program?

gethostbyname()

Thanks

Augie

Somewhere on QUICS there the source to a file called ifconfig_example. It
does
the same thing as ifconfig. That’s a good start.

I thing the specific ioctl to get the address is SIOCGIFADDR (check
sys/ioctl.h for
a list of tcpip related ioctl)

“Augie Henriques” <augiehenriques@hotmail.com> wrote in message
news:8vdsrd$1n0$1@inn.qnx.com

Hi.

Is there a way to get the machine IP address from a C program?

Thanks

Augie

Dean Douthat <ddouthat@faac.com> wrote in message
news:8vdtqb$2bu$1@inn.qnx.com

In article <8vdsrd$1n0$> 1@inn.qnx.com> >, > augiehenriques@hotmail.com > says…

Hi.

Is there a way to get the machine IP address from a C program?

gethostbyname()

How do I get the host name (for current system) to pass to gethostbyname()?

Augie

Thanks

Augie

From unix.h
extern int gethostname(char *buffer, int buffer_length);

Augie Henriques <augiehenriques@hotmail.com> wrote in message news:8vdu9h$2ti$1@inn.qnx.com

Dean Douthat <> ddouthat@faac.com> > wrote in message
news:8vdtqb$2bu$> 1@inn.qnx.com> …
In article <8vdsrd$1n0$> 1@inn.qnx.com> >, > augiehenriques@hotmail.com > says…

Hi.

Is there a way to get the machine IP address from a C program?

gethostbyname()

How do I get the host name (for current system) to pass to gethostbyname()?

Augie Henriques <augiehenriques@hotmail.com> a écrit dans le message :
8vdsrd$1n0$1@inn.qnx.com

Hi.

Is there a way to get the machine IP address from a C program?

Thanks

Augie

This will work:



#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>


#define NAMELEN 255


void main (void)
{
char host_name[NAMELEN];
struct hostent *hp;
char address[16];
struct in_addr addr;

gethostname (host_name, NAMELEN);
if ((hp = gethostbyname (host_name)) != NULL)
{
addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr;
strcpy (address, inet_ntoa (addr));
}
else
strcpy (address, “No entry”);
printf ("%s\n", address);
}





Yanick Aubut
yaubut@sigma-techno.com
Sigma Automatisation Inc.
6, rue Patrice-Côté CP 130
Trois-Pistoles (Qc)
G0L 4K0