Mail Archives: djgpp/1997/06/08/23:35:15
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 -