我遇到了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调用错误时,请说:
使用了与请求的协议不兼容的地址。
我怎么能解决这个错误?我的错误是什么?