Galaxy Kylin (linux architecture aarch64) Get all IP addresses of this machine

environment:

Version: Galaxy Kylin Desktop OS V10 (SP1)
Kernel: Linux 5.4.18-35-generic
CPU: Phytium, D2000/8

Enter uname -m in the terminal to view
the architecture aarch64

On windows and Galaxy Unicorn (linux loongarch64) use the following code to get the IP address:

Galaxy Kylin (linux) gethostname can not get the IP address

The same code works on Galaxy Kylin (linux architecture loongarch64), but not on Galaxy Kylin (aarch64), so read the file to get the list of all ip addresses, the code is as follows:

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;
}

 

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/128494922