IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Windows下获取本地IP地址的两种方法 -> 正文阅读

[网络协议]Windows下获取本地IP地址的两种方法

总结两种获取本地IP地址的方法

第一种:C++代码

CmdExe.h

class CmdExc
{
public:
    CmdExc(std::string cmd, std::string mode = "rt");
    ~CmdExc();

    std::string getOutput() const;
    std::string getLocalIP(std::string ipconfig_content);
private:
    std::string m_strOutput__;
    FILE* m_fp__;
};

CmdExe.cpp

CmdExc::CmdExc(std::string cmd, std::string mode)
{
    m_fp__ = _popen(cmd.c_str(), mode.c_str());
    char buf[256] = { 0 };
    if (NULL != m_fp__) {
        int read_len;
        while ((read_len = fread(buf, sizeof(buf) - 1, 1, m_fp__)) > 0) {
            m_strOutput__ += buf;
            memset(buf, 0, sizeof(buf));
        }
    }

}

CmdExc::~CmdExc()
{
    if (NULL != m_fp__) {
        _pclose(m_fp__);
    }
}

std::string CmdExc::getOutput() const
{
    return m_strOutput__;
}

void trimstring(std::string& str)
{
    if (!str.empty())
    {
        str.erase(0, str.find_first_not_of(" "));
        str.erase(str.find_last_not_of(" ") + 1);
    }
}

std::string CmdExc::getLocalIP(std::string strIpconfig_content)
{
    std::string ip("127.0.0.1");
    auto p = strIpconfig_content.find("IPv4");//查找字符串中第1个出现的"IPv4"
    if (p != std::string::npos)
    {
        auto p2 = strIpconfig_content.find(":", p);
        if (p2 != std::string::npos)
        {
            auto p3 = strIpconfig_content.find("\n", p2);
            if (p3 != std::string::npos)
            {
                ip = strIpconfig_content.substr(p2 + 1, p3 - p2 - 1);
                trimstring(ip);
            }
        }
    }
    
    return ip;
}

第二种:QT代码

#include <Qprocess>

QString getLocalIP()
{
    QString strIpAddress{"127.0.0.1"};
    QProcess cmd_pro;
    QString cmd_str = QString("ipconfig");
    cmd_pro.start("cmd.exe", QStringList() << "/c" << cmd_str);
    cmd_pro.waitForStarted();
    cmd_pro.waitForFinished();
    QString result = cmd_pro.readAll();
    QString pattern("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
    QRegExp rx(pattern);

    int pos = 0;
    bool found = false;
    while ((pos = rx.indexIn(result, pos)) != -1) 
    {
        QString tmp = rx.cap(0);
        //跳过子网掩码 eg:255.255.255.0
        if (-1 == tmp.indexOf("255")) {
            if (strIpAddress != "" && -1 != tmp.indexOf(strIpAddress.mid(0, strIpAddress.lastIndexOf(".")))) {
                found = true;
                break;
            }
            strIpAddress = tmp;
        }
        pos += rx.matchedLength();
    }

    return strIpAddress;
}
int main()
{
	//第一种
    CmdExc cmd("ipconfig");
    std::string strIpconfig_content = cmd.getOutput().c_str();
	std::string strIP = cmd.getLocalIP(strIpconfig_content);
	
	//第二种
	//QString strIP = getLocalIP();
      
    return 0;
}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-02-19 01:33:50  更:2022-02-19 01:35:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/5 11:19:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码