本文整理汇总了C++中TcpConnection::socket方法的典型用法代码示例。如果您正苦于以下问题:C++ TcpConnection::socket方法的具体用法?C++ TcpConnection::socket怎么用?C++ TcpConnection::socket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TcpConnection
的用法示例。
在下文中一共展示了TcpConnection::socket方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sizeof
int Network::Server::initConnection(const char* host, const char* port)
{
if (strlen(host) > INET6_ADDRSTRLEN || strlen(port) > MAX_PORT_LENGTH )
{
DEBUGPRINT("SERVER ERROR:\t Invalid port or host length to init\n");
return -2;
}
if (host == NULL || port == NULL)
{
DEBUGPRINT("SERVER ERROR:\t Invalid arguments to init\n");
}
LOGPRINT("SERVER STATUS:\t Connection to host:%s:%s\n", host, port);
addrinfo l_hints, *l_result, *l_p;
int l_resvalue = -1;
memset(&l_hints, 0, sizeof(struct addrinfo) );
l_hints.ai_family = AF_UNSPEC;
l_hints.ai_socktype = SOCK_STREAM;
if ((l_resvalue = getaddrinfo(host, port, &l_hints, &l_result)) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not get addrinfo: %s\n", gai_strerror(l_resvalue));
return -1;
}
TcpConnection * conn = new TcpConnection();
if (conn == NULL)
{
DEBUGPRINT("SERVER ERROR:\t Could not create new TCP Connection\n");
return -1;
}
for (l_p = l_result; l_p != NULL; l_p = l_p->ai_next)
{
if ((l_resvalue =conn->socket(l_p)) != 0)
{
if (l_resvalue == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not connect socket trying next\n");
} else if (l_resvalue == -2)
{
DEBUGPRINT("SERVER ERROR:\t Argument error to socket\n");
} else
{
DEBUGPRINT("SERVER ERROR:\t Invalid Return\n");
}
continue;
}
if ((l_resvalue = conn->connect(l_p)) != 0)
{
if (l_resvalue == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not connect to server/port\n");
} else if (l_resvalue == -2)
{
DEBUGPRINT("SERVER ERROR:\t Argument error to connect\n");
} else {
DEBUGPRINT("SERVER ERROR:\t Invalid return\n");
}
conn->close();
continue;
}
break;
}
if (l_p == NULL)
{
DEBUGPRINT("SERVER FAILURE:\t Failed to connect to %s:%s\n", host, port);
return -1;
}
int fileDesc = conn->getSocketFd();
int enable = 1;
if (setsockopt(fileDesc, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) == -1)
{
return -1;
}
if ( setnonblock(fileDesc) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not set socket to non-blocking\n");
}
if( addHandler(fileDesc, EPOLLET|EPOLLIN|EPOLLHUP, conn ) == -1)
{
DEBUGPRINT("SERVER ERROR:\t Could not add handler to tcpConnection\n");
return -1;
}
return fileDesc;
//.........这里部分代码省略.........