[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Come ottenere l'IP addr



Risolto.
Dovesse mai tornare utile a qualcuno
ecco la soluzione trovata:

***************************************************************
#include <sys/ioctl.h>
#include <net/if.h>
#include < unistd.h>
#include <arpa/inet.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
 int i;
 char *ip;
 int s = socket (PF_INET, SOCK_STREAM, 0);

      for (i=1;;i++) {
          struct ifreq ifr;
          struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;

          ifr.ifr_ifindex = i;
          if (ioctl (s, SIOCGIFNAME, &ifr) < 0)
             break;

          /* now ifr.ifr_name is set */
          if (ioctl (s, SIOCGIFADDR, &ifr) < 0)
             continue;

          ip = inet_ntoa (sin->sin_addr);
             printf ("%s\n", ip);
       
          if (ioctl (s, SIOCGIFNETMASK, &ifr) < 0)
             continue;

           ip = inet_ntoa (sin->sin_addr);
              printf ("%s\n", ip);
       }

       close (s);
       return 0;
}

Tratta direttamente da:

http://www.hungry.com/~alves/local-ip-in-C.html

Perdonate la parentesi OT....
I.

--
"By all means let's be open-minded,
but not so open-minded that our brains drop out."
(Richard Dawkins)
Reply to: