微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > host entry的问题

host entry的问题

时间:10-02 整理:3721RD 点击:
void main()
{
    struct in_addr addr;
    struct hostent *ent;
    char host[128];
    char *ip;
    gethostname(host, 128);
    printf("get host: %s\n", host);
    ent = gethostbyname(host);
    if(ent == NULL)
    {
        printf("fuck error\n");
        return;
    }
    memcpy((char *)&addr, (char*)ent->h_addr_list[1], (size_t)ent->h_length);
    ip = inet_ntoa(addr);
    printf("get ip: %s\n", ip);
    printf("what we get: %s\n", ent->h_name);
}
得到的ip address和ifconfig得到的不一样!
xxx@xxx ~/test $ hostname -i
202.102.110.203

xxx@xxx~/test $ ./test
get host: HUS2
get ip: 202.102.110.203
what we get: HUS2

ifconfig:
inet 172.28.30.9  netmask 255.255.255.0  broadcast 172.28.30.255

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top