判断用户输入IP的合法性&判断输入IP与本机IP是否在同一网段C++&QT

目录

一、任务目标

二、判断用户输入IP的合法性

三、判断用户输入IP与本机IP是否在同一网段


一、任务目标

1、判断用户输入IP的合法性,例如是否不符合IP地址的正确格式;

2、判断用户输入IP与本机IP是否在同一网段;

3、使用C++和QT实现;

二、判断用户输入IP的合法性

cpp 复制代码
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

bool isValidIPAddress(const std::string& ipAddress) {
    std::vector<int> ipSegments;
    std::istringstream iss(ipAddress);
    std::string segment;
    
    while (std::getline(iss, segment, '.')) {
        try {
            int value = std::stoi(segment);
            if (value < 0 || value > 255) {
                return false;
            }
            ipSegments.push_back(value);
        } catch (const std::exception& e) {
            return false;
        }
    }
    
    return ipSegments.size() == 4;
}

int main() {
    std::string text = "192.168.1.3:502";
    size_t delimiterPos = text.find(':');
    std::string ipAddress = delimiterPos != std::string::npos ? text.substr(0, delimiterPos) : text;
    
    if (isValidIPAddress(ipAddress)) {
        std::cout << "The IP address is valid." << std::endl;
    } else {
        std::cout << "The IP address is invalid." << std::endl;
    }
    
    return 0;
}

三、判断用户输入IP与本机IP是否在同一网段

cpp 复制代码
//使用Visual Studio
//项目属性->"Qt Project Settings"->"Qt Modules"->勾选"network"
#include <QtNetwork/QNetworkInterface>
#include <QtNetwork/QNetworkAddressEntry>
#include <QtNetwork/QAbstractSocket>

//获取本机IP
QHostAssress hostIP()
{
    auto list = QNetworkInterface::allInterfaces();
    foreach(QNetworkInterface interface,list)
    {
        //1、首先判断是不是以太网,过滤Wifi
        if(interface.type() != QNetworkInterface::Ethernet)
            continue;

        //2、如果有安装虚拟机VMware,会出现虚拟网卡,需要过滤
        if(interface.humanReadableName().contains("VMware"))
            continue;

        //3、一般会有两个IP地址,一ipv4一个ipv6,过滤ipv6
        foreach(auto entry,interface.addressEntries())
        {
            if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol)
                return entry.ip();
        }
    }
    return QHostAddress();
}

int main()
{
    //获取本机IP
    QHostAddress LocalIP = hostIP();
    QString ipAddress = LocalIP.toString();
    std::string text = ipAddress.toStdString();
    //获取本机网段值
    int extractedInt = -1;
    std::string delimiter = ".";
    std::vector<std::string> tokens;
    std::string token;
    std::istringstream tokenStream(text);

    while (std::getline(tokenStream, token, '.')) {
        tokens.push_back(token);
    }

    if (tokens.size() >= 3) {
        std::string extracted = tokens[2];
        extractedInt = std::stoi(extracted);
    }

    //获取用户输入IP的网段值
    std::string InputIP = "192.168.1.3:502";
    int extractedInt2 = -2;
    std::vector<std::string> tokens2;
    std::string token2;
    std::istringstream tokenStream2(InputIP);

    while (std::getline(tokenStream2, token2, '.')) {
        tokens2.push_back(token2);
    }

    if (tokens2.size() >= 3) {
        std::string extracted2 = tokens2[2];
        extractedInt2 = std::stoi(extracted2);
    }

    //判断用户输入的IP是否与本机IP在同一网段
    if(extractedInt == extractedInt2)
    {
        std::cout<<"在同一网段"<<std::endl;
    }
    else
    {
        std::cout<<"不在同一网段"<<std::endl;
    }
    
    return 0;
}

上述两项功能可以结合在一起使用,在"判断用户输入IP的合法性"后"判断用户输入IP与本机IP是否在同一网段"。

上述功能因笔者使用场景为QT的交互界面所以用到了QT的QtNetwork模块。

//上述代码为笔者在网页编写,如有错误望及时提出,感谢!

相关推荐
Want5959 分钟前
C/C++跳动的爱心①
c语言·开发语言·c++
lingggggaaaa19 分钟前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
phdsky31 分钟前
【设计模式】建造者模式
c++·设计模式·建造者模式
H_-H32 分钟前
关于const应用与const中的c++陷阱
c++
coderxiaohan38 分钟前
【C++】多态
开发语言·c++
gfdhy1 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
leon_zeng01 小时前
Qt Modern OpenGL 入门:从零开始绘制彩色图形
开发语言·qt·opengl
会飞的胖达喵1 小时前
Qt CMake 项目构建配置详解
开发语言·qt
ceclar1231 小时前
C++范围操作(2)
开发语言·c++
一个不知名程序员www2 小时前
算法学习入门---vector(C++)
c++·算法