0

我对 winsock2 api 函数的 getnameinfo 有疑问。

我有一个名为 CISocket 的类,其中包含这样的文件:

vector<char>* ip;

并具有用于创建名为的套接字的功能:

CreateSocket(const vector<char>* ipA,unsigned short nPort,SocketType stype,bool nonBlocking,bool bBindSocket)
{
    ...
    struct addrinfo *result = 0;
struct addrinfo *ptr = 0;
//The addrinfo structure is used by the getaddrinfo function to hold host address information
struct addrinfo hints;
char local_hostname[1025];
const vector<char>* host = ipA;

    ....
    ZeroMemory(&hints,sizeof(hints));

    hints.ai_family = IsIPv46Socket() ? AF_UNSPEC : (IsIPv6Socket() ? AF_INET6 : AF_INET);
hints.ai_socktype = IsStream() ? SOCK_STREAM : SOCK_DGRAM;
hints.ai_protocol = IsStream() ? IPPROTO_TCP : IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE
    #if !IRNET_UNDER_WINDOWS || _WIN32_WINNT>=0x600
    | AI_ADDRCONFIG
    #endif

    getaddrinfo(local_hostname,port,&hints,&result)
    for (ptr=result;ptr;ptr=result->ai_next)
{
         ....
         vector<char>::pointer chPtr = ip->data();
        if (status = GetIPAndPort(this->fd,/*&(*this->ip)*/&(*chPtr),&this->port))
        {
            char* err = gai_strerrorA(status);
            cout<< "Failed To Get Local IP and port.";
            CloseSocket(&this->fd);
            continue;
        }           
    }
}

从这个函数内部,我调用另一个名为 GetIPAndPort 的方法

GetIPAndPort( int32_t fd,__out /*vector<char>*/char* ip,__out uint16_t* nPort )
{
if (nPort)
{
    *nPort = 0;
}

if (this->fd > 0)
{
    int status;
    struct sockaddr_storage ss;//retrieved transport address holder
    if (status = GetSocketAddress(this->fd,&ss))
    {
        //cout<< "GetSocketAddress has failed with status code: %d", status"
        return -1;
    }
    int ret = GetSocketIPAndPort((struct sockaddr*)&ss,ip,nPort);
    return ret;
}
}

此方法出现的主要问题:

GetSocketIPAndPort( struct sockaddr* ss, char* pIP, uint16_t* nPort )
{
int status = -1;
/*typedef char ip_t[65];
ip_t* pip ={0};
vector<char>* chr = new vector<char>(65);
vector<char>::pointer ptr = pIP->data();*/
if (ss->sa_family == AF_INET)
{
    sockaddr_in *sin = (struct sockaddr_in*)ss;
    if (nPort)
    {
        *nPort = Endianness::HostToNetwork(sin->sin_port);
        status = 0;
    }
    if (pIP)
    {
        if (status = getnameinfo((struct sockaddr*)&sin,sizeof(sockaddr),pIP,/*sizeof(*pIP)*/1025,0,0,NI_NUMERICHOST))
        {
            char* serr = gai_strerrorA(status);
            return status;
        }
    }
}
}

在这个方法中,当 getnameinfo 调用一个错误时说:

使用了与请求的协议不兼容的地址。

我该如何解决这个错误?我的错误是什么?

4

0 回答 0