zoukankan      html  css  js  c++  java
  • c、c++混编实现查询本地IP地址

    一、思路

      1、要想得到本地IP地址,可以通过本机名来查询,所以首先得得到本机名。

      2、牵涉到IP地址,首先想到牵涉到网络协议,因此得加载套接字协议,所以先使用WSAStartup函数完成对Winsock服务的初始化。

    二、步骤

      

    三、实现程序的模块化

      1、包含文件工作

    #include <iostream.h>
    #include <winsock2.h>
    #include <windows.h>
    #pragma comment(lib,"ws2_32.lib")
    
    bool GetLocalAddr();

      2、网络协议初始化工作

    WSADATA wsaData;
    WORD wVersionRequested;
    wVersionRequested = MAKEWORD(2,2);
    int initWSA =  WSAStartup(wVersionRequested,&wsaData);

      3、通过主机名来获取本地连接的IP地址

    •   hostent是host entry的缩写,该结构记录主机的信息,包括主机名、别名、地址类型、地址长度和地址列表。
    if (0 == initWSA)
        {
            cout << "初始化完成!" << endl;
            
            // 获取主机名
            char hostName[255];
            int iRet = gethostname(hostName,sizeof(hostName));
            if (iRet != 0)
            {
                cout << "获取主机名失败!" << endl;
                return false;
            }
            
            // 通过主机名获取地址
    // hostent *hostInfo; hostInfo = gethostbyname(hostName); if (NULL == hostInfo) { cout << "通过主机名获取地址失败!" << endl; return false; } // 将网络地址转换成字符串,以便观看 char *IPAddr; IPAddr = inet_ntoa(*(struct in_addr *)*hostInfo->h_addr_list); cout << IPAddr << endl; // 卸载Winsock库,并释放所有资源 WSACleanup(); return true; } else return false;


    四、完整的程序

    #include <iostream.h>
    #include <winsock2.h>
    #include <windows.h>
    #pragma comment(lib,"ws2_32.lib")
    
    bool GetLocalAddr();
    
    int main(void)
    {
        GetLocalAddr();
        return 0;
    }
    
    bool GetLocalAddr()
    {
        // 初始化Winsock库
        WSADATA wsaData;
        WORD wVersionRequested;
        wVersionRequested = MAKEWORD(2,2);
        int initWSA =  WSAStartup(wVersionRequested,&wsaData);
    
        if (0 == initWSA)
        {
            cout << "初始化完成!" << endl;
            
            // 获取主机名
            char hostName[255];
            int iRet = gethostname(hostName,sizeof(hostName));
            if (iRet != 0)
            {
                cout << "获取主机名失败!" << endl;
                return false;
            }
            
            // 通过主机名获取地址
            hostent *hostInfo;
            hostInfo = gethostbyname(hostName);
            if (NULL == hostInfo)
            {
                cout << "通过主机名获取地址失败!" << endl;
                return false;
            }
    
            // 将网络地址转换成字符串,以便观看
            char *IPAddr;
            IPAddr = inet_ntoa(*(struct in_addr *)*hostInfo->h_addr_list);
            cout << IPAddr << endl;
    
            // 卸载Winsock库,并释放所有资源
            WSACleanup();
            
            return true;
        }
        else
            return false;
    }
  • 相关阅读:
    [Leetcode] Set Matrix Zeroes
    [Leetcode] Longest Valid Parentheses
    [Leetcode] Interleaving String
    [Leetcode] Surrounded Regions
    [Leetcode] Candy
    用Delphi获取当前系统时间
    Delphi窗体中禁用最大化按钮
    DELPHI关于文件的操作
    Delphi 2010初体验,是时候抛弃Delphi 7了
    双通道内存有什么优点和缺点?
  • 原文地址:https://www.cnblogs.com/aoguren/p/3178731.html
Copyright © 2011-2022 走看看