当前位置: 首页>>代码示例>>C++>>正文


C++ HostInfo::SetErrorString方法代码示例

本文整理汇总了C++中HostInfo::SetErrorString方法的典型用法代码示例。如果您正苦于以下问题:C++ HostInfo::SetErrorString方法的具体用法?C++ HostInfo::SetErrorString怎么用?C++ HostInfo::SetErrorString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HostInfo的用法示例。


在下文中一共展示了HostInfo::SetErrorString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Lookup


//.........这里部分代码省略.........
            saSize = sizeof(sa6);
            memset(&sa6, 0, sizeof(sa6));
            sa6.sin6_family = AF_INET6;
            memcpy(sa6.sin6_addr.s6_addr, address.GetIPv6Address().data, sizeof(sa6.sin6_addr.s6_addr));
            sa6.sin6_port = htons(portNum);
        }

        // unknown (should be unreachable)
        else BT_ASSERT_X(false, "HostInfo::Lookup: unknown network protocol");

        // lookup name for IP
        char hbuf[NI_MAXHOST];
        char serv[NI_MAXSERV];
        if ( sa && (getnameinfo(sa, saSize, hbuf, sizeof(hbuf), serv, sizeof(serv), 0) == 0) )
            result.SetHostName(string(hbuf));

        // if no domain name found, just use the original address's IP string
        if ( result.HostName().empty() )
            result.SetHostName(address.GetIPString());

        // store address in HostInfo
        uniqueAddresses.insert(address);
    }

    // otherwise, hostname is a domain name ('www.foo.bar')
    // do 'normal' lookup
    else {

        // setup address lookup 'hints'
        addrinfo hints;
        memset(&hints, 0, sizeof(hints));
        hints.ai_family   = AF_UNSPEC;   // allow either IPv4 or IPv6
        hints.ai_socktype = SOCK_STREAM; // for TCP
        hints.ai_protocol = IPPROTO_TCP;

        // fetch addresses for requested hostname/port
        addrinfo* res;
        int status = getaddrinfo(hostname.c_str(), port.c_str(), &hints, &res );

        // if everything OK
        if ( status == 0 ) {

            // iterate over all IP addresses found
            addrinfo* p = res;
            for ( ; p != NULL; p = p->ai_next ) {

                // IPv4
                if ( p->ai_family == AF_INET ) {
                    sockaddr_in* ipv4 = (sockaddr_in*)p->ai_addr;
                    HostAddress a( ntohl(ipv4->sin_addr.s_addr) );
                    uniqueAddresses.insert(a);
                }

                // IPv6
                else if ( p->ai_family == AF_INET6 ) {
                    sockaddr_in6* ipv6 = (sockaddr_in6*)p->ai_addr;
                    HostAddress a(ipv6->sin6_addr.s6_addr);
                    uniqueAddresses.insert(a);
                }
            }

            // if we iterated, but no addresses were stored
            if ( uniqueAddresses.empty() && (p == NULL) ) {
                result.SetError(HostInfo::UnknownError);
                result.SetErrorString("HostInfo: unknown address types found");
            }
        }

        // handle error cases
        else if (
#ifndef _WIN32
                     status == EAI_NONAME
                  || status == EAI_FAIL
#  ifdef EAI_NODATA
                  || status == EAI_NODATA  // officially deprecated, but just in case we happen to hit it
#  endif // EAI_NODATA

#else  // _WIN32
                     WSAGetLastError() == WSAHOST_NOT_FOUND
                  || WSAGetLastError() == WSANO_DATA
                  || WSAGetLastError() == WSANO_RECOVERY
#endif // _WIN32
                )
        {
            result.SetError(HostInfo::HostNotFound);
            result.SetErrorString("HostInfo: host not found");
        }
        else {
            result.SetError(HostInfo::UnknownError);
            result.SetErrorString("HostInfo: unknown error encountered");
        }

        // cleanup
        freeaddrinfo(res);
    }

    // store fetched addresses (converting set -> vector) in result & return
    result.SetAddresses( vector<HostAddress>(uniqueAddresses.begin(), uniqueAddresses.end()) );
    return result;
}
开发者ID:AlistairNWard,项目名称:premo,代码行数:101,代码来源:HostInfo_p.cpp


注:本文中的HostInfo::SetErrorString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。