Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 显示IPv6地址_C++_Visual C++ - Fatal编程技术网

C++ 显示IPv6地址

C++ 显示IPv6地址,c++,visual-c++,C++,Visual C++,我正在写一个程序来显示机器的本地IP地址。 我可以显示IPv4地址,但无法显示IPv6地址。 下面是我用来显示IPv4地址的程序: #include <iostream.h> #include <winsock.h> int doit(int, char **) { char ac[80]; if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) { cerr << "Error "

我正在写一个程序来显示机器的本地IP地址。 我可以显示IPv4地址,但无法显示IPv6地址。 下面是我用来显示IPv4地址的程序:

#include <iostream.h>
#include <winsock.h>

int doit(int, char **)
{
    char ac[80];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        cerr << "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
    cout << "Host name is " << ac << "." << endl;

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Yow! Bad host lookup." << endl;
        return 1;
    }

    for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
    }

    return 0;
}

int main(int argc, char *argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }

    int retval = doit(argc, argv);

    WSACleanup();

    return retval;
}
#包括
#包括
int doit(int,char**)
{
char-ac[80];
if(gethostname(ac,sizeof(ac))==SOCKET\u错误){

cerr
gethostbyname
已过时,在许多系统上会忽略IPv6条目


使用现代功能并检查
ai_INET
ai_INET6
ai_家族成员,以识别地址类型。

如前一回答所述,使用getaddrinfo()此外,由于您使用C++,请确保您使用RAII进行清理,因此即使抛出异常也会进行清理。
if ( (n = getaddrinfo(host.c_str(), service.c_str(), &hints, &res)) != 0) {
    ostringstream ss;

    ss << "getaddrinfo error for " << host << ", " << service
       << ", " << gai_strerror(n);

    throw std::runtime_error(ss.str());
}

// Make sure freeaddrinfo is called even if exceptions are thrown.                                                                                                                                                                       
// Note that we do not need to check res for NULL, unique_ptr handles that                                                                                                                                                               
// when deallocating.                                                                                                                                                                                                                    
auto cleanup = [](addrinfo* ai) { freeaddrinfo(ai); };
unique_ptr<addrinfo, decltype(cleanup)> aip(res, cleanup);
if((n=getaddrinfo(host.c_str()、service.c_str()、提示和res))!=0){
ostringstream ss;
党卫军