写一个code,来检测本机lP和其他的设备有IP冲突,如有则获取一个新的ip

cpp 复制代码
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <string.h>
#include <vector>
#include <unistd.h>
bool isIpConflict(const std::string& localIp, const std::vector<std::string>& ipList) {
    for (const std::string& ip : ipList) {
        if (ip != localIp) {
            return true;
        }
    }
    return false;
}

bool isIpUsed(const std::string& ip) {
    bool is_used = false;
    struct sockaddr_in sa;
    size_t len = sizeof(sa);

    if (inet_pton(AF_INET, ip.c_str(), &sa) == 0) {
        is_used = true;
    } else {
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd != -1) {
            if (connect(fd, (struct sockaddr*)&sa, len) == 0) {
                is_used = true;
            }
            close(fd);
        }
    }

    return is_used;
}

// 从start到end之间的每个IP地址依次判断是否被使用,直到找到第一个未被使用的IP地址并输出
const std::string getNewIp(const std::string& ip) {
    const int start = 10;   // 起始IP地址(十进制)
    const int end = 254;     // 结束IP地址(十进制)
    const std::string subnet = ip.substr(0, ip.find_last_of('.')) + '.';  // IP地址前缀
    for (int i = start; i <= end; ++i) {
        std::string newIp = subnet + std::to_string(i);
        if (!isIpUsed(newIp)) {
            return newIp;
        }
    }
    return "";
}

int main() {
    // 本地的IP地址:192.168.159.137
    struct ifaddrs *ifaddr;
    std::vector<std::string> ipList{"192.168.159.136","192.168.159.137"}; // 模拟所获得的其他设备IP列表
    // 调用 getifaddrs() 函数获取所有网络接口信息
    if (getifaddrs(&ifaddr) == -1) {
        perror("Error getting network interfaces");
        return 1;
    }
    
    // 遍历每个网络接口
    for (struct ifaddrs* it = ifaddr; it != nullptr; it = it->ifa_next) {
        
        // 只处理 IPv4 类型的接口
        if (it->ifa_addr && it->ifa_addr->sa_family == AF_INET) {
            char ip[INET_ADDRSTRLEN];
            
            // 将 IPv4 地址转换为字符串格式并输出
            inet_ntop(AF_INET, &((sockaddr_in*)it->ifa_addr)->sin_addr, ip, INET_ADDRSTRLEN);
            if(strcmp(it->ifa_name,"ens33")==0) {
                std::cout << "Interface: " << it->ifa_name << ", IP Address: " << ip << std::endl;
                if(isIpConflict(ip,ipList)) {
                    std::cout<<"检测出本机的IP地址和其他的设备有ip地址起冲突了"<<std::endl;
                    std::cout << "获得未被使用的新IP为:"<< getNewIp(ip) << std::endl;
                }
            }
                
        }
    }
    freeifaddrs(ifaddr);
    return 0;
}
cpp 复制代码
Interface: ens33, IP Address: 192.168.159.137
检测出本机的IP地址和其他的设备有ip地址起冲突了
获得未被使用的新IP为:192.168.159.10

此题为小编自己思考做出来的,没有正确答案,仅供参考,欢迎一起学习和交流!

相关推荐
是娇娇公主~6 小时前
HTTPS【密钥交换+证书校验】流程讲解
网络·网络协议·面试·https·ssl
驰羽7 小时前
NAT模式下VMware的虚拟机DNS解析失败的问题解决
linux·网络·dns
誰能久伴不乏8 小时前
epoll 学习踩坑:`fcntl` 设置非阻塞到底用 `F_SETFL` 还是 `F_SETFD`?
linux·服务器·网络·c++·tcp/ip
北京耐用通信9 小时前
告别“蜘蛛网”接线!耐达讯自动化PROFIBUS 三路集线器让气缸布线“一拖三”的神操作
人工智能·物联网·网络协议·自动化·信息与通信
云川之下9 小时前
【网络】华为交换机S3700与S5700详解
服务器·网络·华为
小于晏9 小时前
基于Socket实现的主流网络协议汇总
网络·网络协议
tianyuanwo10 小时前
深入理解iptables:规则管理与匹配机制深度解析
网络·安全·web安全
TG:@yunlaoda360 云老大10 小时前
如何使用华为云国际站代理商WSA配置与架构交付中的安全策略?
网络·架构·华为云
打码人的日常分享10 小时前
企业数据资产管控和数据治理解决方案
大数据·运维·网络·人工智能·云计算
阿华hhh10 小时前
Linux系统编程(网络udp)
linux·服务器·c语言·网络·网络协议·udp