本文整理汇总了C++中SockAddr::toNative方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::toNative方法的具体用法?C++ SockAddr::toNative怎么用?C++ SockAddr::toNative使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::toNative方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bind
SocketError SocketImpl::bind(const SockAddr& sockAddr) {
if (socket == INVALID_SOCKET) {
return SocketError::NotInitialized;
}
sockaddr_storage storage;
sockAddr.toNative(&storage);
// For bind we also want to set the SO_EXCLUSIVEADDRUSE option
DWORD opt = 1;
int optRes = ::setsockopt(socket, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char*)&opt, sizeof(DWORD));
if (optRes != 0) {
return getSocketError(::WSAGetLastError());
}
int bindRes = ::bind(socket, (const sockaddr*)&storage, sizeof(sockaddr_storage));
if (bindRes != 0) {
return getSocketError(::WSAGetLastError());
}
// And then we need to fetch back the bound addr/port
int nameLen = sizeof(sockaddr_storage);
int nameRes = ::getsockname(socket, (sockaddr*)&storage, &nameLen);
if (nameRes != 0) {
return getSocketError(::WSAGetLastError());
}
localAddr = SockAddr::fromNative(&storage);
return SocketError::Ok;
}
示例2: connect
SocketError SocketImpl::connect(const SockAddr& sockAddr) {
if (socket == INVALID_SOCKET) {
return SocketError::NotInitialized;
}
sockaddr_storage storage;
sockAddr.toNative(&storage);
// For reasons that I presume have to do with DisconnectEx, ConnectEx
// requires a bound socket
sockaddr_storage addrAny;
addrAny.ss_family = storage.ss_family;
INETADDR_SETANY((sockaddr*)&addrAny);
int bindRes = ::bind(socket, (const sockaddr*)&addrAny, sizeof(sockaddr_storage));
if (bindRes != 0) {
return getSocketError(::WSAGetLastError());
}
SocketError res;
connect_co(sockAddr, &res).get();
return res;
}