我正在研究一些网络代码。我在同一台机器上运行了2个解决方案。第一个是我的服务器,另一个是我的客户端。我正在使用C ++和winsock来构建网络解决方案。
这是我的服务器代码(设置)。
addrinfo hints;
addrinfo* pAddrInfo;
char pIPAddress[INET6_ADDRSTRLEN];
char pPortFinal[128];
unsigned long tempUL;
int error;
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC;//AF_INET; // don't care IPv4 or IPv6
hints.ai_socktype = type;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
error = getaddrinfo(NULL/*"localhost"*/, pPort, &hints, &pAddrInfo);
if(error != 0)
{
GetWinSockError(error);
return;
}
for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo->ai_next)
{
this->m_Socket = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if(bind(this->m_Socket, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen) != 0)
continue;
break;
}
if(!pAddrInfo)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
//7 = magic number recommended by 'beej's guide' online
if(listen(this->m_Socket, 7) != 0)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
getnameinfo(pAddrInfo->ai_addr, pAddrInfo->ai_addrlen, pIPAddress, sizeof pIPAddress, pPortFinal, sizeof pPortFinal, NI_NUMERICHOST | NI_NUMERICSERV);
this->m_pIPAddress = Helpers::String::NewCopy(pIPAddress);
this->m_pPort = Helpers::String::NewCopy(pPortFinal);
cout << "IP:" << this->m_pIPAddress << " Port:" << this->m_pPort << "\n";
this->SetState(NET_COMM_STATE__READY);
freeaddrinfo(pAddrInfo);
这是我的服务器代码(勾选)。
NetServerClient* pNetComm;
sockaddr* pSockAddr;
sockaddr_storage their_addr;
socklen_t addr_size;
void* pTempBuffer;
char pIPAddress[INET6_ADDRSTRLEN];
char pPort[256];
unsigned short port;
int newClientSocket;
addr_size = sizeof their_addr;
newClientSocket = accept(this->m_Socket, (struct sockaddr*)(&their_addr), &addr_size);
if(newClientSocket == -1)
{
if(WSAGetLastError() != WSAEWOULDBLOCK)
GetWinSockError(WSAGetLastError());
return;
}
pSockAddr = (struct sockaddr*)(&their_addr);
pNetComm = new NetServerClient(newClientSocket);
addr_size = sizeof their_addr;
if(bind(newClientSocket, pSockAddr, addr_size) != 0)
{
GetWinSockError(WSAGetLastError());
pNetComm->ShutDown();
return;
}
if (pSockAddr->sa_family == AF_INET)
{
pTempBuffer = &(((struct sockaddr_in*)pSockAddr)->sin_addr);
port = ((struct sockaddr_in*)pSockAddr)->sin_port;
}
else
{
pTempBuffer = &(((struct sockaddr_in6*)pSockAddr)->sin6_addr);
port = ((struct sockaddr_in6*)pSockAddr)->sin6_port;
}
inet_ntop(their_addr.ss_family, pTempBuffer, pIPAddress, sizeof pIPAddress);
_snprintf(pPort, 256, "%d", (unsigned int)port);
pNetComm->StartCommunication(pIPAddress, pPort);
this->m_vpClients.push_back(pNetComm);
这是我的客户代码:
addrinfo hints;
addrinfo* pAddrInfo;
unsigned long tempUL;
int error;
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = type;
error = getaddrinfo(pIPAddress, pPort, &hints, &pAddrInfo);
if(error != 0)
{
GetWinSockError(error);
return;
}
for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo->ai_next)
{
this->m_Socket = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if(connect(this->m_Socket, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen) != 0)
continue;
break;
}
if(!pAddrInfo)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
this->SetState(NET_COMM_STATE__READY);
freeaddrinfo(pAddrInfo);
问题是客户端上的连接总是失败。我收到错误&#34; WSAECONNREFUSED:连接被拒绝&#34;。
我很确定我的连接失败的原因是因为我的客户端没有将正确的IP地址传递给getaddrinfo。当我运行我的服务器时,我输出服务器解决方案的IP和端口。然后我让我的客户端解决方案使用相同的IP和端口信息调用它的getaddrinfo。但是,我的服务器解决方案似乎没有收集正确的IP和端口信息。它总是告诉我它的IP是0.0.0.0(IPv4)或::(IPv6)。我不明白如何为客户端连接到我的服务器获取正确的IP。
我已经阅读了这些帖子Getting server ip 0.0.0.0:0 from getaddrinfo()和sin_port returned in getaddrinfo wrong并尝试了他们的解决方案,但他们并没有为我工作。
非常感谢任何帮助。