银河麒麟(linux 架构aarch64) 获取本机所有IP地址

环境:

版本:银河麒麟桌面操作系统V10(SP1)
内核:Linux 5.4.18-35-generic
CPU:Phytium,D2000/8

终端输入 uname -m 查看
架构 aarch64

在windows 上 和 银河麒麟(linux loongarch64) 使用下面的代码获取IP地址:

银河麒麟(linux) gethostname 获取不到IP地址_程序媛zcx的博客-CSDN博客_银河麒麟查看ip地址

同样的代码 在银河麒麟(linux 架构loongarch64)可以,但是在 银河麒麟(aarch64)上不可以,于是用读文件的方式获取所有ip地址列表,代码如下:

std::vector<std::string> WHUDP:: getIpList() {
    std::vector<std::string> result;
    char buff[BUFSIZ];
    int i=0,k=0;
    char tmpbuff[1];
    int ret=0;
    FILE * addrFile;
    addrFile = popen("ifconfig", "r");
    memset(buff,0,BUFSIZ);
    while(1) {
        ret = fread(tmpbuff, 1, 1, addrFile);
        if(ret)
        {
            buff[i]=tmpbuff[0];
            i++;
            if(buff[i - 1]=='\n')
            {
                string str = buff;
                string::size_type idx = str.find("inet ");
//                cout<< "buff111111== " << buff << " idx== " << to_string(idx) << endl;
                if(idx == string::npos) {
                } else {
//                    cout<< "buff222== " << buff  << endl;
//                    stringstream strsplite(str);
//                    char c = ' ';
//                    vector<string> resArr;
//                    string ss1;
//                    while(getline(strsplite, ss1, c)) {
//                        cout<< "ss1=== " << ss1 << endl;
//                        resArr.push_back(ss1);
//                    }
//                    string ipstr = resArr[9];
//                    cout<< " ipstr11===  " << ipstr << endl;

//                    result.push_back(ipstr);

                    int start = idx+5;
                    for(k=start;k< strlen(buff);k++)
                    {
                        if(buff[k]==' ')
                        {
//                            cout<< "===========" << to_string(k-(start)) << endl;
                            string ipstr=str.substr(start,k-(start));
//                            cout<< " ipstr11===" << ipstr << endl;
                            result.push_back(ipstr);
                            k=0;
                            i=0;
                            memset(buff,0,BUFSIZ);
                            break;
                        }
                    }
                }
                k=0;
                i=0;
                memset(buff,0,BUFSIZ);
            }
        }
        else
        {
            break;
        }

    }
    fclose(addrFile);

    return result;
}

猜你喜欢

转载自blog.csdn.net/qq_40015157/article/details/128494922