本文整理汇总了C++中NetworkAddress::network_address方法的典型用法代码示例。如果您正苦于以下问题:C++ NetworkAddress::network_address方法的具体用法?C++ NetworkAddress::network_address怎么用?C++ NetworkAddress::network_address使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkAddress
的用法示例。
在下文中一共展示了NetworkAddress::network_address方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: tcp_open
DTerr DeviceNetworkSockets::tcp_open (NetworkSocket &socket, const NetworkAddress &ip, DTboolean is_server)
{
struct sockaddr_storage sast = string_to_address(ip.network_address());
struct sockaddr *sa = (struct sockaddr *) &sast;
socklen_t sas;
// Build the socket
DTint socket_raw = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket_raw < 0) {
socket_raw = -1;
LOG_MESSAGE << "TCPOpen: socket: " << strerror(errno) << " (" << (DTuint) errno << ")";
return DT3_ERR_NET_UNKNOWN;
}
switch (sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
#if DT3_OS == DT3_ANDROID
sas = sizeof(sockaddr);
#else
sas = sin->sin_len;
#endif
} break;
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
#if DT3_OS == DT3_ANDROID
sas = sizeof(sockaddr_in6);
#else
sas = sin6->sin6_len;
#endif
} break;
}
if (is_server) {
DTerr err = ::bind(socket_raw, (const sockaddr *) sa, sas);
if (err != 0) {
::close(socket_raw);
LOG_MESSAGE << "TCPOpen: bind: " << strerror(errno) << " (" << (DTuint) errno << ")";
return DT3_ERR_NET_UNKNOWN;
}
err = ::listen(socket_raw, 5); // Arbitrary backlog value
if (err != 0) {
::close(socket_raw);
LOG_MESSAGE << "TCPOpen: listen: " << strerror(errno) << " (" << (DTuint) errno << ")";
return DT3_ERR_NET_UNKNOWN;
}
} else {
DTint err = ::connect(socket_raw, (const sockaddr *) sa, sas);
if (err != 0) {
::close(socket_raw);
LOG_MESSAGE << "TCPOpen: connect: " << strerror(errno) << " (" << (DTuint) errno << ")";
return DT3_ERR_NET_UNKNOWN;
}
}
// set up non-blocking reads and writes
DTint val = ::fcntl(socket_raw, F_GETFL, 0);
::fcntl(socket_raw, F_SETFL, val | O_NONBLOCK);
socket.set_network_socket_data((DTubyte*) &socket_raw, sizeof(socket_raw));
return DT3_ERR_NONE;
}