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


C++ NetworkAddress::set_network_address方法代码示例

本文整理汇总了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;
	}
	
}
开发者ID:9heart,项目名称:DT3,代码行数:32,代码来源:DeviceNetworkSockets.cpp

示例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;
}
开发者ID:9heart,项目名称:DT3,代码行数:31,代码来源:DeviceNetworkSockets.cpp


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