首頁>Club>
8
回覆列表
  • 1 # CFM澤北

    解決辦法:

    #include <stdio.h>

    #include <stdlib.h>

    #include <string.h>

    #include <net/if.h>

    #include <net/if_arp.h>

    #include <sys/ioctl.h>

    #include <netinet/in.h>

    void err_sys(const char *errmsg);

    int getmac(char *out,int sockfd)

    {

    int i;

    struct ifreq ifr;

    struct arpreq arpr;

    char *p;

    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));

    /* get hardware address */

    ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;

    if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)

    return -1;

    /* output hardware address */

    p=out;

    for (i = 0; i < 6; i++) {

    unsigned char *mac = (unsigned char *) ifr.ifr_hwaddr.sa_data;

    p+=sprintf(p,"%02x", (int) mac);

    if (i != 5)

    p+=sprintf(p,"%c", ":");

    }

    return 0;

    }

  • 2 # 和平精英老袁

    GetIfEntry GetIfTable

    下面是例項

    http://www.codeproject.com/KB/IP/BytesSentReceived.aspx

  • 3 # 稻殼

    bool GetIPV4Gateway(const char * pNICName, char *pGateway, unsigned long len)

    {

    char buffer[200] = { 0 };

    unsigned long bufLen = sizeof(buffer);

    unsigned long defaultRoutePara[4] = { 0 };

    FILE * pfd = fopen(PATH_ROUTE, "r");

    if (NULL == pfd)

    {

    return false;

    }

    while (fgets(buffer, bufLen, pfd))

    {

    sscanf(buffer, "%*s %x %x %x %*x %*x %*x %x %*x %*x %*x\n", (unsigned int *)&defaultRoutePara[1], (unsigned int *)&defaultRoutePara[0], (unsigned int *)&defaultRoutePara[3], (unsigned int *)&defaultRoutePara[2]);

    if (NULL != strstr(buffer, pNICName))

    {

    //如果FLAG標誌中有 RTF_GATEWAY

    if (defaultRoutePara[3] & RTF_GATEWAY)

    {

    unsigned long ip = defaultRoutePara[0];

    snprintf(pGateway, len, "%d.%d.%d.%d", (ip & 0xff), (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff);

    break;

    }

    }

    memset(buffer, 0, bufLen);

    }

    fclose(pfd);

    pfd = NULL;

    return true;

    }

    這裡PATH_ROUTE是/proc/net/route

    RTF_GATEWAY需要包括標頭檔案#include <net/route.h>

    假設要獲取wifi的閘道器,比如wlan0

    char gateway[32] = { 0 };

    GetIPV4Gateway("wlan0", gateway, 32);

  • 中秋節和大豐收的關聯?
  • 古人云:好漢無好妻,醜漢娶仙女。咱當今有誰應了這句話,還特別有故事呢?