当前位置: 首页>>代码示例>>C++>>正文


C++ SocketAddress::ToSockAddr方法代码示例

本文整理汇总了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;
}
开发者ID:Jazeido,项目名称:ola,代码行数:46,代码来源:TCPSocket.cpp

示例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,
//.........这里部分代码省略.........
开发者ID:DanielAeolusLaude,项目名称:ola,代码行数:101,代码来源:TCPSocket.cpp


注:本文中的SocketAddress::ToSockAddr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。