用程序如何判断主板上是双网卡还是单网卡呢?
用程序如何判断主板上是双网卡还是单网卡呢? 问题点数:50、回复次数:2Top
1 楼vipiii(vipiii)回复于 2005-09-27 17:50:44 得分 50
写了段代码,供参考。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#define SIZE (1024)
int get_interface_list(const struct ifconf *list)
{
int sockfd = 0;
if (list == NULL) {
printf("wrong arguments\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("error to create socket\n");
return -2;
}
if (ioctl(sockfd, SIOCGIFCONF, list) < 0) {
perror("error to get list\n");
close(sockfd);
return -3;
}
close(sockfd);
return 0;
}
int main(int argc, char **argv)
{
struct ifreq buf[SIZE];
struct ifreq *ptr = NULL;
struct ifconf list;
int i = 0;
memset(buf, 0, sizeof(buf));
list.ifc_len = sizeof(buf);
list.ifc_buf = (char *)buf;
if (get_interface_list(&list) < 0) {
return -1;
}
for (i = 0; i < SIZE; i++) {
ptr = &buf[i];
if (ptr->ifr_name[0] == '\0') {
break;
}
printf("interface_%d: %s\n", i+1, ptr->ifr_name);
}
return 0;
}
Top
2 楼vipiii(vipiii)回复于 2005-09-27 17:53:12 得分 0
计算一下有多少个ethX...Top




