本文整理汇总了C++中HostInfo::Addresses方法的典型用法代码示例。如果您正苦于以下问题:C++ HostInfo::Addresses方法的具体用法?C++ HostInfo::Addresses怎么用?C++ HostInfo::Addresses使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HostInfo
的用法示例。
在下文中一共展示了HostInfo::Addresses方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ConnectImpl
bool TcpSocket::ConnectImpl(const HostInfo& hostInfo,
const std::string& port,
IBamIODevice::OpenMode mode)
{
// skip if we're already connected
if ( m_state == TcpSocket::ConnectedState ) {
m_error = TcpSocket::SocketResourceError;
m_errorString = "socket already connected";
return false;
}
// reset socket state
m_hostName = hostInfo.HostName();
m_mode = mode;
m_state = TcpSocket::UnconnectedState;
m_error = TcpSocket::UnknownSocketError;
// m_localPort = 0;
m_remotePort = 0;
// m_localAddress.Clear();
m_remoteAddress.Clear();
m_readBuffer.Clear();
// fetch candidate addresses for requested host
vector<HostAddress> addresses = hostInfo.Addresses();
if ( addresses.empty() ) {
m_error = TcpSocket::HostNotFoundError;
m_errorString = "no IP addresses found for host";
return false;
}
// convert port string to integer
stringstream ss(port);
uint16_t portNumber(0);
ss >> portNumber;
// iterate through adddresses
vector<HostAddress>::const_iterator addrIter = addresses.begin();
vector<HostAddress>::const_iterator addrEnd = addresses.end();
for ( ; addrIter != addrEnd; ++addrIter) {
const HostAddress& addr = (*addrIter);
// try to initialize socket engine with this address
if ( !InitializeSocketEngine(addr.GetProtocol()) ) {
// failure to initialize is OK here
// we'll just try the next available address
continue;
}
// attempt actual connection
if ( m_engine->Connect(addr, portNumber) ) {
// if connection successful, update our state & return true
m_mode = mode;
// m_localAddress = m_engine->GetLocalAddress();
// m_localPort = m_engine->GetLocalPort();
m_remoteAddress = m_engine->GetRemoteAddress();
m_remotePort = m_engine->GetRemotePort();
m_cachedSocketDescriptor = m_engine->GetSocketDescriptor();
m_state = TcpSocket::ConnectedState;
return true;
}
}
// if we get here, no connection could be made
m_error = TcpSocket::HostNotFoundError;
m_errorString = "could not connect to any host addresses";
return false;
}