本文整理汇总了C++中NetworkAddress::set_network_address方法的典型用法代码示例。如果您正苦于以下问题:C++ NetworkAddress::set_network_address方法的具体用法?C++ NetworkAddress::set_network_address怎么用?C++ NetworkAddress::set_network_address使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkAddress
的用法示例。
在下文中一共展示了NetworkAddress::set_network_address方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: resolve_host
DTerr DeviceNetworkSockets::resolve_host (NetworkAddress &ip, const std::string &host, DTushort port)
{
std::string real_host = Globals::substitute_global(host);
struct addrinfo hints, *res;
// get address info
::bzero(&hints, sizeof(hints));
#if defined(DT3_USE_IPV6) && DT3_USE_IPV6
hints.ai_family = AF_UNSPEC;
#else
hints.ai_family = AF_INET;
#endif
hints.ai_socktype = SOCK_STREAM;
DTint err = ::getaddrinfo( (host.size() == 0) ? NULL : real_host.c_str(), MoreStrings::cast_to_string(port).c_str(), &hints, &res);
if (err != 0) {
return DT3_ERR_NET_UNKNOWN;
}
// save the results for freeing later
if (res != NULL) {
ip.set_network_address(address_to_string(res->ai_addr));
::freeaddrinfo(res);
return DT3_ERR_NONE;
} else {
::freeaddrinfo(res);
return DT3_ERR_NET_UNKNOWN;
}
}
示例2: tcp_accept
DTerr DeviceNetworkSockets::tcp_accept (NetworkSocket &connection_socket, NetworkAddress &ip, const NetworkSocket &listen_socket)
{
if (listen_socket.is_empty())
return DT3_ERR_NONE;
DTint listen_socket_raw = *reinterpret_cast<DTint*>(listen_socket.network_socket_data());
struct sockaddr_storage sast;
struct sockaddr *sa = (struct sockaddr *) &sast;
socklen_t sas = sizeof(sast);
DTint connection_socket_raw = ::accept(listen_socket_raw, (sockaddr *) sa, &sas);
if (connection_socket_raw < 0) {
if (errno == EWOULDBLOCK) {
return DT3_ERR_NET_WOULD_BLOCK;
}
LOG_MESSAGE << "TCPAccept: accept: " << strerror(errno) << " (" << (DTuint) errno << ")";
return DT3_ERR_NET_UNKNOWN;
}
// set up non-blocking reads and writes
DTint val = ::fcntl(connection_socket_raw, F_GETFL, 0);
::fcntl(connection_socket_raw, F_SETFL, val | O_NONBLOCK);
connection_socket.set_network_socket_data((DTubyte*) &connection_socket_raw, sizeof(connection_socket_raw));
ip.set_network_address(address_to_string( (const struct sockaddr *) sa));
return DT3_ERR_NONE;
}