[vox-tech] getting IP address in C

Ryan vox-tech@lists.lugod.org
Wed, 16 Apr 2003 05:59:58 -0700


My machine has two IPs I guess. 127.0.0.1 and 64.167.231.194.
I want to be able to detect the 64.167.231.194.

Here is what I ended up doing to solve the problem

        /*detecting hostname and ip*/
        printf("Getting hostname....\n");
        gethostname(hostname, 256);
        printf("Found %s... sound good to you?\n", hostname);

        printf("Getting IP Address now...\n");
        h_entry = gethostbyname(hostname);
        if(h_entry == 0) crash("gethostbyname()");

        printf("Aha! found %s\n", inet_ntoa(*((struct in_addr
*)h_entry->h_addr)));
        inet_aton(h_entry->h_addr, &adr_server.sin_addr);

        len_inet = sizeof(adr_server);
        /*************************/


-ryan


----- Original Message -----
From: "Rod Roark" <rod@sunsetsystems.com>
To: <vox-tech@lists.lugod.org>
Sent: Wednesday, April 16, 2003 4:54 AM
Subject: Re: [vox-tech] getting IP address in C


> On Wednesday 16 April 2003 02:41 am, Ryan wrote:
> > Is there a quick and easy way in C to get the IP address of the current
> > machine the program is running on?
>
> Depends on what you want to do.  The problem with your
> question is that a machine can have multiple IP addresses.
>
> Using 127.0.0.1 will usually work if you want to open a
> connection to something running on the local machine.
>
> Otherwise you can try gethostname() and resolve it to an IP
> with gethostbyname().
>
> --
> Rod Roark, Sunset Systems     http://www.sunsetsystems.com/
> Offering preconfigured Linux computers, custom software and
> remote system administration services.
> Public Key: http://www.sunsetsystems.com/rodspublickey.asc
>
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
>