|
|
发表于 2006-4-20 15:16:02
|
显示全部楼层
抄的:
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
/* maximum NICs, change this as neccessary */
#define MAXINTERFACES 16
int main (int argc, char *argv[])
{
int fd, n, r = 0;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
/* try to open a AF_INET socket */
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
exit(1);
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
/* try to get all interface configuration information */
if (ioctl (fd, SIOCGIFCONF, (char *) &ifc)!=0)
exit(2);
/* the number of NIC cards */
n = ifc.ifc_len / sizeof (struct ifreq);
while (n-- > 0)
{
/* printf ("net device %s\n", buf[n].ifr_name); */
/* get the IP address of this NIC card */
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[n])))
puts(inet_ntoa(((struct sockaddr_in*)(&buf[n].ifr_addr))->sin_addr));
else ++r;
}
close (fd);
return r;
} |
|