本文整理汇总了C++中SockAddr::getFamily方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::getFamily方法的具体用法?C++ SockAddr::getFamily怎么用?C++ SockAddr::getFamily使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::getFamily方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getIpPortFromSockAddr
IpPort getIpPortFromSockAddr(const SockAddr &sa){
const int family = sa.getFamily();
if(family == AF_INET){
if(sa.getSize() < sizeof(::sockaddr_in)){
LOG_POSEIDON_WARNING("Invalid IPv4 SockAddr: size = ", sa.getSize());
DEBUG_THROW(Exception, sslit("Invalid IPv4 SockAddr"));
}
char ip[INET_ADDRSTRLEN];
const char *const str = ::inet_ntop(AF_INET,
&static_cast<const ::sockaddr_in *>(sa.getData())->sin_addr, ip, sizeof(ip));
if(!str){
DEBUG_THROW(SystemException);
}
return IpPort(SharedNts(str),
loadBe(static_cast<const ::sockaddr_in *>(sa.getData())->sin_port));
} else if(family == AF_INET6){
if(sa.getSize() < sizeof(::sockaddr_in6)){
LOG_POSEIDON_WARNING("Invalid IPv6 SockAddr: size = ", sa.getSize());
DEBUG_THROW(Exception, sslit("Invalid IPv6 SockAddr"));
}
char ip[INET6_ADDRSTRLEN];
const char *const str = ::inet_ntop(AF_INET6,
&static_cast<const ::sockaddr_in6 *>(sa.getData())->sin6_addr, ip, sizeof(ip));
if(!str){
DEBUG_THROW(SystemException);
}
return IpPort(SharedNts(str),
loadBe(static_cast<const ::sockaddr_in6 *>(sa.getData())->sin6_port));
}
LOG_POSEIDON_WARNING("Unknown IP protocol ", family);
DEBUG_THROW(Exception, sslit("Unknown IP protocol"));
}
示例2: make_tuple
static
std::tuple<Socket, SocketError> getConnectedSocket(const SockAddr sockAddr) {
Socket requestSocket;
SocketError socketErr = requestSocket.init(sockAddr.getFamily());
if (socketErr != SocketError::Ok) {
return std::make_tuple(Socket(), socketErr);
}
socketErr = requestSocket.connect(sockAddr);
if (socketErr != SocketError::Ok) {
return std::make_tuple(Socket(), socketErr);
}
return std::make_tuple(std::move(requestSocket), SocketError::Ok);
}