delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/08/23:35:15

Date: Mon, 9 Jun 1997 11:28:41 +0800 (GMT)
From: Orlando Andico <orly AT gibson DOT eee DOT upd DOT edu DOT ph>
To: Michael Åström <michael DOT astrom AT erm DOT ericsson DOT se>
cc: djgpp AT delorie DOT com
Subject: Re: TCP/IP, what is my IP address?
In-Reply-To: <33954849.2EAA@erm.ericsson.se>
Message-ID: <Pine.SGI.3.93.970609112252.22353A-100000@gibson.eee.upd.edu.ph>
MIME-Version: 1.0

On Wed, 4 Jun 1997, Michael Åström wrote:

> Sorry, but I think you missunderstod me. The programs task is to figure
> out its own IP-address. And therefor I need a C scource that does so.
> Please, post me one.

Uhm. If you're using UNIX, you can do something like this:

---
 #include <netdb.h>

 struct hostent entry, *tmpentry;
 tmpentry = gethostbyname ("localhost");
 if (tmpentry == NULL)
 {
   fprintf (stderr, "couldn't resolve hostname: localhost\n");
   exit (1);
 }
 else
   memcpy (&entry, tmpentry, sizeof (struct hostent));
---

At which point entry.h_addr_list contains the list of IP addresses
(char **h_addr_list) terminated with a null (i.e. first IP address is
h_addr_list[0] and so on). These are 4-bytes long for IPv4, network-byte
order (not yet dotted-decimal). You can use the inet_network() function to
retrieve the dotten-decimal address.

If you're using Winsock/Windows.. I haven't got a clue  :)
 

-----------------------------------------------------------------------
Orlando Alcantara Andico
WWW:   http://www2.mozcom.com/~orly/           Email:   orly AT mozcom DOT com
ICBM:  14 deg. 30' N, 120 deg. 59' E           POTS:    (+632) 932-2385

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019