获取本地IP地址

#include <iostream>
#include <string>
#include <Winsock2.h>

using namespace std;

#pragma comment(lib, "ws2_32.lib");

int main()
{
    //
    //加载套接字***************************************************************
    //
    WORD wVersion = MAKEWORD(1, 1);
    WSADATA wsaData;
    int err;

    err = WSAStartup(wVersion, &wsaData);
    if (err != 0)
    {
        cout << "加载套接字失败" << endl;
        system("pause");
        return -1;
    }

    if (LOBYTE(wsaData.wVersion) != 1 ||
        HIBYTE(wsaData.wVersion) != 1)
    {
        WSACleanup();
        cout << "加载套接字版本不符!" << endl;
        system("pause");
        return -1;
    }

    //
    //get host name************************************************************
    //
    char host_name[256];
    memset(host_name, 0, sizeof(host_name));
    int result = gethostname(host_name, sizeof(host_name));
    if (result == 0)
    {
        cout << "host name: " << host_name << endl;
    }
    else
    {
        cout << "get host name error!" << endl;
        system("pause");
        return -1;
    }

    //
    //get host by name*********************************************************
    //
    //hostent* pHostEnt = gethostbyname(host_name);
    hostent* pHostEnt = gethostbyname("www.google.com");
    //hostent* pHostEnt = gethostbyname("www.baidu.com");
    //hostent* pHostEnt = gethostbyname("www.sohu.com");
    if (pHostEnt == NULL)
    {
        cout << "get host by name error!" << endl;
        system("pause");
        return -1;
    }

    //offical name of host
    cout << "offical name of host: " << pHostEnt->h_name << endl;   

    //alias---别名
    //有多个或者零个,比如:本机就是0个,www.google.com就有1个,www.sohu.com就有两个
    cout << "aliases: " << endl;
    for (char** aliases=pHostEnt->h_aliases; *aliases != NULL; aliases++)
    {
        cout << *aliases << endl;
    }  

    //host address type and length of address
    cout << "address type and length of address: " << endl;
    cout << pHostEnt->h_addrtype << endl;    //AF_INET(2)或者AF_INET6(23)
    cout << pHostEnt->h_length << endl;      //对应AF_INET值为4,对应AF_INET6值为6

    //ip address
    cout << "ip address: " << endl;
    for (char** addr_list=pHostEnt->h_addr_list; *addr_list!=NULL; ++addr_list)
    {
        in_addr addr;
        memcpy(&addr, *addr_list, 4);
        string str_ip_addr = inet_ntoa(addr);
        cout << str_ip_addr << endl;

        //(1)绝妙啊,发现自己还是挺聪明的,哈哈...
        //如果:str_ip_addr为10.142.247.95
        //则这里分别输出10 \n 142 \n 247 \n 95
        for (int i=0; i<4; ++i)
        {
            cout << (unsigned short)(unsigned char)(*addr_list)[i] << endl;
        }

        //(2)相较于(1),繁琐了许多
        //for (int i=0; i<4; ++i)
        //{
        //    unsigned short s = 0;
        //    memcpy((char*)&s, (*addr_list)+i, 1);
        //    cout << s << endl;
        //}
    }

    //
    //卸载套接字,退出程序******************************************************
    //
    WSACleanup();
    system("pause");

    return 0;
}

注意:
如果网络断开的话,gethostbyname如果是获取本地的host信息,则OK
但是返回的IP为本地回路地址:127.0.0.1(未断开时返回的IP地址诸如10.142.247.95)
如果是获取网络上的诸如www.google.com的host信息,则gethostbyname会出错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值