删除文章

确定要删除这篇文章吗?

取消
确定

获取本地IP地址

     阅读(48)  2020-04-28 02:38:07

首先来一种windows和linux平台都支持的

#ifdef WIN32
#include <boost/asio.hpp>
#include <boost/asio/io_service.hpp>
#else
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#endif

std::string getLocalIpAddress(const std::string &slash) {
        static bool s_once = true;
        static std::string s_result;

        if (s_once) {
            s_once = false;
#ifdef WIN32
            using boost::asio::ip::tcp;
            try {
                boost::asio::io_service io_service;
                tcp::resolver resolver(io_service);
                tcp::resolver::query query(tcp::v4(), boost::asio::ip::host_name(), "");
                tcp::resolver::iterator iter = resolver.resolve(query);
                tcp::resolver::iterator end; // End marker.
                while (iter != end) {
                    tcp::endpoint ep = *iter++;
                    if (!s_result.empty()) {
                        s_result += slash;
                    }
                    s_result += ep.address().to_string();
                }
            }
            catch (std::exception &e) {
                std::cerr << "get local ip address failed, " << e.what() << std::endl;
            }
#else
            struct ifaddrs * ifAddrStruct = NULL;
            struct ifaddrs * ifa = NULL;
            void * tmpAddrPtr = NULL;

            getifaddrs(&ifAddrStruct);
            for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
                if (!ifa->ifa_addr) {
                    continue;
                }
                if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
                                                           // is a valid IP4 Address
                    tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
                    char addressBuffer[INET_ADDRSTRLEN];
                    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
                    std::string address(addressBuffer);
                    if (address != "127.0.0.1") {
                        if (!s_result.empty()) {
                            s_result += slash;
                        }
                        s_result += address;
                    }
                }
            }
            if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
#endif
        }
        return s_result;
    }

再来看一下Qt的写法,看起来清爽多了

    QString getLocalHost()
    {
        QStringList ipList;
        const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
        for (const QHostAddress &address : QNetworkInterface::allAddresses()) {
            if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
            ipList << address.toString();
        }
        return ipList.join("-");
    }

文章评论

Keep it simple,stupid
文章数
329
今日访问
3108
今日IP数
229
最近评论

liangzi: 不错 谢谢分享
tujiaw: registerThreadInactive:如果当前没有激活的线程,就去激活线程,让等待的线程去执行任务。
hgzzx: 佩服佩服。 请教:registerThreadInactive的作用是什么?
xuehaoyun: 很不错,来围观一下
tujiaw: 抱歉csdn code服务关闭了,这个代码我也找不到了
于淞: 你好,这个文章的源码能分享一下吗,songsong9181@163.com,谢谢了 上面的写错了
回到顶部