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


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

本文整理汇总了C++中SocketAddress::GetLength方法的典型用法代码示例。如果您正苦于以下问题:C++ SocketAddress::GetLength方法的具体用法?C++ SocketAddress::GetLength怎么用?C++ SocketAddress::GetLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SocketAddress的用法示例。


在下文中一共展示了SocketAddress::GetLength方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: bind

bool
SocketDescriptor::BindPort(unsigned port)
{
  SocketAddress address = SocketAddress::MakePort4(port);

  return bind(Get(), address, address.GetLength()) == 0;
}
开发者ID:MindMil,项目名称:XCSoar,代码行数:7,代码来源:SocketDescriptor.cpp

示例2: Connect

bool SocketImpl::Connect(const SocketAddress& address, const std::chrono::seconds& timeout /*= std::chrono::seconds(0)*/)
{
    if (INVALID_SOCKET == m_sockfd)
        Init(AF_INET);
    if (timeout.count() > 0)
        SetBlocking(false);
    bool bResult = false;
    do 
    {
        if (SOCKET_ERROR == connect(m_sockfd, address.GetAddress(), address.GetLength()))
        {
            if (timeout.count() == 0)
                break;
            int err = WSAGetLastError();
            if (WSAEINPROGRESS != err
                && WSAEWOULDBLOCK != err
                && WSAEISCONN != err)
                break;
            if (!Poll(timeout, SELECT_READ | SELECT_WRITE | SELECT_ERROR))
                break;
            if (GetSocketError() != 0)
                break;
        }
        bResult = true;
    } while (0);
    if (timeout.count() > 0)
        SetBlocking(true);
    return bResult;
}
开发者ID:whrool,项目名称:Net,代码行数:29,代码来源:SocketImpl.cpp

示例3: assert

bool
SocketDescriptor::Connect(const SocketAddress &address)
{
  assert(address.IsDefined());

  return ::connect(Get(), address, address.GetLength()) >= 0;
}
开发者ID:MindMil,项目名称:XCSoar,代码行数:7,代码来源:SocketDescriptor.cpp

示例4: Bind

bool SocketImpl::Bind(const SocketAddress& address, bool bReuse /*= false*/)
{
    if (INVALID_SOCKET == m_sockfd)
        Init(AF_INET);
    if (bReuse)
        SetReuseAddress(true);
    return bind(m_sockfd, address.GetAddress(), address.GetLength()) != SOCKET_ERROR;
}
开发者ID:whrool,项目名称:Net,代码行数:8,代码来源:SocketImpl.cpp

示例5:

ssize_t
SocketDescriptor::Write(const void *buffer, size_t length,
                        const SocketAddress &address)
{
  return ::sendto(Get(), (const char *)buffer, length,
#ifdef HAVE_POSIX
#ifdef __linux__
                  MSG_DONTWAIT|MSG_NOSIGNAL,
#else
                  MSG_DONTWAIT,
#endif
#else
                  0,
#endif
                  address, address.GetLength());
}
开发者ID:MindMil,项目名称:XCSoar,代码行数:16,代码来源:SocketDescriptor.cpp

示例6: Connect

Error UDPSocket::Connect(llvm::StringRef name, bool child_processes_inherit,
                         Socket *&send_socket, Socket *&recv_socket) {
  std::unique_ptr<UDPSocket> final_send_socket;
  std::unique_ptr<UDPSocket> final_recv_socket;

  Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
  if (log)
    log->Printf("UDPSocket::%s (host/port = %s)", __FUNCTION__, name.data());

  Error error;
  std::string host_str;
  std::string port_str;
  int32_t port = INT32_MIN;
  if (!DecodeHostAndPort(name, host_str, port_str, port, &error))
    return error;

  // Setup the receiving end of the UDP connection on this localhost
  // on port zero. After we bind to port zero we can read the port.
  final_recv_socket.reset(new UDPSocket(child_processes_inherit, error));
  if (error.Success()) {
    // Socket was created, now lets bind to the requested port
    SocketAddress addr;
    addr.SetToAnyAddress(AF_INET, 0);

    if (::bind(final_recv_socket->GetNativeSocket(), addr, addr.GetLength()) ==
        -1) {
      // Bind failed...
      SetLastError(error);
    }
  }

  assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
  if (error.Fail())
    return error;

  // At this point we have setup the receive port, now we need to
  // setup the UDP send socket

  struct addrinfo hints;
  struct addrinfo *service_info_list = nullptr;

  ::memset(&hints, 0, sizeof(hints));
  hints.ai_family = kDomain;
  hints.ai_socktype = kType;
  int err = ::getaddrinfo(host_str.c_str(), port_str.c_str(), &hints,
                          &service_info_list);
  if (err != 0) {
    error.SetErrorStringWithFormat(
#if defined(_MSC_VER) && defined(UNICODE)
        "getaddrinfo(%s, %s, &hints, &info) returned error %i (%S)",
#else
        "getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
#endif
        host_str.c_str(), port_str.c_str(), err, gai_strerror(err));
    return error;
  }

  for (struct addrinfo *service_info_ptr = service_info_list;
       service_info_ptr != nullptr;
       service_info_ptr = service_info_ptr->ai_next) {
    auto send_fd = CreateSocket(
        service_info_ptr->ai_family, service_info_ptr->ai_socktype,
        service_info_ptr->ai_protocol, child_processes_inherit, error);
    if (error.Success()) {
      final_send_socket.reset(new UDPSocket(send_fd));
      final_send_socket->m_send_sockaddr = service_info_ptr;
      break;
    } else
      continue;
  }

  ::freeaddrinfo(service_info_list);

  if (!final_send_socket)
    return error;

  send_socket = final_send_socket.release();
  recv_socket = final_recv_socket.release();
  error.Clear();
  return error;
}
开发者ID:CodaFi,项目名称:swift-lldb,代码行数:81,代码来源:UDPSocket.cpp

示例7: SendTo

int SocketImpl::SendTo(const char* buffer, int length, const SocketAddress& address, int flags /*= 0*/)
{
    assert(INVALID_SOCKET != m_sockfd);
    return sendto(m_sockfd, buffer, length, flags, address.GetAddress(), address.GetLength());
}
开发者ID:whrool,项目名称:Net,代码行数:5,代码来源:SocketImpl.cpp

示例8: UdpConnect

Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket)
{
    std::unique_ptr<Socket> final_send_socket;
    std::unique_ptr<Socket> final_recv_socket;
    NativeSocket final_send_fd = kInvalidSocketValue;
    NativeSocket final_recv_fd = kInvalidSocketValue;

    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
    if (log)
        log->Printf ("Socket::UdpConnect (host/port = %s)", host_and_port.data());

    Error error;
    std::string host_str;
    std::string port_str;
    int32_t port = INT32_MIN;
    if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
        return error;

    // Setup the receiving end of the UDP connection on this localhost
    // on port zero. After we bind to port zero we can read the port.
    final_recv_fd = ::CreateSocket (AF_INET, SOCK_DGRAM, 0, child_processes_inherit);
    if (final_recv_fd == kInvalidSocketValue)
    {
        // Socket creation failed...
        SetLastError (error);
    }
    else
    {
        final_recv_socket.reset(new Socket(final_recv_fd, ProtocolUdp, true));

        // Socket was created, now lets bind to the requested port
        SocketAddress addr;
        addr.SetToAnyAddress (AF_INET, 0);

        if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
        {
            // Bind failed...
            SetLastError (error);
        }
    }

    assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
    if (error.Fail())
        return error;

    // At this point we have setup the receive port, now we need to 
    // setup the UDP send socket

    struct addrinfo hints;
    struct addrinfo *service_info_list = NULL;

    ::memset (&hints, 0, sizeof(hints)); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_DGRAM;
    int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list);
    if (err != 0)
    {
        error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", 
                                       host_str.c_str(), 
                                       port_str.c_str(),
                                       err,
                                       gai_strerror(err));
        return error;        
    }

    for (struct addrinfo *service_info_ptr = service_info_list; 
         service_info_ptr != NULL; 
         service_info_ptr = service_info_ptr->ai_next) 
    {
        final_send_fd = ::CreateSocket (service_info_ptr->ai_family,
                                        service_info_ptr->ai_socktype,
                                        service_info_ptr->ai_protocol,
                                        child_processes_inherit);

        if (final_send_fd != kInvalidSocketValue)
        {
            final_send_socket.reset(new Socket(final_send_fd, ProtocolUdp, true));
            final_send_socket->m_udp_send_sockaddr = service_info_ptr;
            break;
        }
        else
            continue;
    }

    :: freeaddrinfo (service_info_list);

    if (final_send_fd == kInvalidSocketValue)
    {
        SetLastError (error);
        return error;
    }

    send_socket = final_send_socket.release();
    recv_socket = final_recv_socket.release();
    error.Clear();
    return error;
}
开发者ID:JuliaLang,项目名称:lldb,代码行数:97,代码来源:Socket.cpp


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