直接扔代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
using namespace std;
void show_all_if()
{
int fdSock = 0;
struct ifconf ifMyConf;
struct ifreq ifMyReq;
char szBuf[20480] = {0};
char * ip;
ifMyConf.ifc_len = 2048;
ifMyConf.ifc_buf = szBuf;
if((fdSock = socket (AF_INET, SOCK_DGRAM, 0)) < 0 )
{
cout << "socket error" <<endl;
return;
}
if(ioctl (fdSock, SIOCGIFCONF, &ifMyConf))
{
close(fdSock);
cout << "ioctl error \n"<<endl;
return;
}
ifreq *it = ifMyConf.ifc_req;
const struct ifreq * const end = it + (ifMyConf.ifc_len / sizeof(struct ifreq));
for(;it != end; ++