本文整理汇总了C++中SocketAddress::ToSockAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ SocketAddress::ToSockAddr方法的具体用法?C++ SocketAddress::ToSockAddr怎么用?C++ SocketAddress::ToSockAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketAddress
的用法示例。
在下文中一共展示了SocketAddress::ToSockAddr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Listen
/*
* Start listening
* @param endpoint the SocketAddress to listen on
* @param backlog the backlog
* @return true if it succeeded, false otherwise
*/
bool TCPAcceptingSocket::Listen(const SocketAddress &endpoint, int backlog) {
struct sockaddr server_address;
int reuse_flag = 1;
if (m_sd != ola::io::INVALID_DESCRIPTOR)
return false;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address)))
return false;
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
OLA_WARN << "socket() failed: " << strerror(errno);
return false;
}
int ok = setsockopt(sd,
SOL_SOCKET,
SO_REUSEADDR,
reinterpret_cast<char*>(&reuse_flag),
sizeof(reuse_flag));
if (ok < 0) {
OLA_WARN << "can't set reuse for " << sd << ", " << strerror(errno);
close(sd);
return false;
}
if (bind(sd, &server_address, sizeof(server_address)) == -1) {
OLA_WARN << "bind to " << endpoint << " failed, " << strerror(errno);
close(sd);
return false;
}
if (listen(sd, backlog)) {
OLA_WARN << "listen on " << endpoint << " failed, " << strerror(errno);
return false;
}
m_sd = sd;
return true;
}
示例2: Listen
/*
* Start listening
* @param endpoint the SocketAddress to listen on
* @param backlog the backlog
* @return true if it succeeded, false otherwise
*/
bool TCPAcceptingSocket::Listen(const SocketAddress &endpoint, int backlog) {
struct sockaddr server_address;
int reuse_flag = 1;
if (m_handle != ola::io::INVALID_DESCRIPTOR)
return false;
if (!endpoint.ToSockAddr(&server_address, sizeof(server_address)))
return false;
int sd = socket(endpoint.Family(), SOCK_STREAM, 0);
if (sd < 0) {
OLA_WARN << "socket() failed: " << strerror(errno);
return false;
}
SocketCloser closer(sd);
#ifdef _WIN32
ola::io::DescriptorHandle temp_handle;
temp_handle.m_handle.m_fd = sd;
temp_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
if (!ola::io::ConnectedDescriptor::SetNonBlocking(temp_handle)) {
#else
if (!ola::io::ConnectedDescriptor::SetNonBlocking(sd)) {
#endif
OLA_WARN << "Failed to mark TCP accept socket as non-blocking";
return false;
}
int ok = setsockopt(sd,
SOL_SOCKET,
SO_REUSEADDR,
reinterpret_cast<char*>(&reuse_flag),
sizeof(reuse_flag));
if (ok < 0) {
OLA_WARN << "can't set reuse for " << sd << ", " << strerror(errno);
return false;
}
if (bind(sd, &server_address, sizeof(server_address)) == -1) {
OLA_WARN << "bind to " << endpoint << " failed, " << strerror(errno);
return false;
}
if (listen(sd, backlog)) {
OLA_WARN << "listen on " << endpoint << " failed, " << strerror(errno);
return false;
}
#ifdef _WIN32
m_handle.m_handle.m_fd = closer.Release();
m_handle.m_type = ola::io::SOCKET_DESCRIPTOR;
#else
m_handle = closer.Release();
#endif
return true;
}
/*
* Stop listening & close this socket
* @return true if close succeeded, false otherwise
*/
bool TCPAcceptingSocket::Close() {
bool ret = true;
if (m_handle != ola::io::INVALID_DESCRIPTOR) {
#ifdef _WIN32
if (closesocket(m_handle.m_handle.m_fd)) {
#else
if (close(m_handle)) {
#endif
OLA_WARN << "close() failed " << strerror(errno);
ret = false;
}
}
m_handle = ola::io::INVALID_DESCRIPTOR;
return ret;
}
/*
* Accept new connections
* @return a new connected socket
*/
void TCPAcceptingSocket::PerformRead() {
if (m_handle == ola::io::INVALID_DESCRIPTOR)
return;
while (1) {
struct sockaddr_in cli_address;
socklen_t length = sizeof(cli_address);
#ifdef _WIN32
int sd = accept(m_handle.m_handle.m_fd, (struct sockaddr*) &cli_address,
//.........这里部分代码省略.........