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


C++ TcpConnection::connect方法代码示例

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


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

示例1: connect

int NetWork::connect(const char *ip, unsigned short port, bool encrypt)
{
	int handle = -1;
	TcpConnection *conn = new TcpConnection(this);

//	if(encrypt) conn->set_encrypt();
	if (conn->connect(ip, port)) {
		if (_online_user.addconn(conn)) {
			_sock_event->add_event(conn, true, true);
			handle = conn->gethandle();
		}
	} else {
		LOGI("NetWork::connect connect fail : %s", strerror(errno));
		//如果连接失败,将底层的网络错误码的负值返回回去;
		handle = (conn->last_sys_errno() * (-1));
		delete conn;
	}

	return handle;
}
开发者ID:Hidebush,项目名称:podText,代码行数:20,代码来源:network.cpp

示例2: 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;
//.........这里部分代码省略.........
开发者ID:hardeep,项目名称:Antix,代码行数:101,代码来源:Server.cpp


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