#include <stdio.h>
#include <string.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<net/if.h>
#include "errno.h"
int main(void)
{
int sock_fd;
struct ifreq buf[18];
struct ifconf ifc;
int interface_num;
ifc.ifc_len = sizeof(buf);
ifc.ifc_req = buf;
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0)
{
printf("socket fail. [%s]\n", strerror(errno));
return -1;
}
if(ioctl(sock_fd, SIOCGIFCONF, (char*)&ifc) < 0)
{
printf("ioctl SIOCGIFCONF fail. [%s]\n", strerror(errno));
close(sock_fd);
return -1;
}
interface_num = ifc.ifc_len/sizeof(struct ifreq);
printf("interface_num = %d\n", interface_num);
while(interface_num--)
{
printf("interfaceName = %s\n", buf[interface_num].ifr_name);
}
close(sock_fd);
return 0;
}
socket查询存在的网卡接口
最新推荐文章于 2023-04-12 15:34:03 发布