Comments (1)
由于没有win10系统暂时不能确认,分析了一下可能的原因仅供参考
调用链如下
void NetHost::GetAddress( string &ip, int &port )
{
m_pConnect->GetAddress(ip, port);
return;
}
void NetConnect::GetAddress( string &ip, int &port )
{
if ( !this->m_bIsServer ) m_socket.GetPeerAddress( ip, port );
else m_socket.GetLocalAddress( ip, port );
return;
}
void Socket::GetPeerAddress( std::string& strWanIP, int& nWanPort )
{
nWanPort = m_nWanPort;
strWanIP = m_strWanIP;
return;
}
bool Socket::InitPeerAddress()
{
if ( INVALID_SOCKET == m_hSocket ) return false;
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
socklen_t nSockAddrLen = sizeof(sockAddr);
if ( SOCKET_ERROR == getpeername( m_hSocket,
(sockaddr*)&sockAddr, &nSockAddrLen ) ) return false;
m_nWanPort = ntohs(sockAddr.sin_port);
m_strWanIP = inet_ntoa(sockAddr.sin_addr);
return true;
}
原因:可能是InitPeerAddress()中getpeername api在不同系统下行为不一致引起
解决方案:
系统win7系统下使用getpeername接口获取IP可以,但win10却不行,获取的错误码为10014.
查看MSDN对应的描述(https://msdn.microsoft.com/en-us/library/ms738533(v=vs.85).aspx):
WSAEFAULT(10014):The name or the namelen parameter is not in a valid part of the user address space, or the namelen parameter is too small.
大致的意思就是缓冲区给小了。
方案1:将sockaddr_in sockAddr; 改为sockaddr_in address1[2]接口正常可以使用。
方案2:用getsockname代替getpeername
from micro-development-kit.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micro-development-kit.