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


C++ CBaseSocket::ConnectToServer方法代码示例

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


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

示例1: GetLastError

int  CCommunication::CheckHeart4PhoneS1(CBaseSocket  & ttBSocket , SOCKET & sendSocket)
{
	//建立连接
	CString  strShowInfo;

	int nRet = -1;

	nRet = ttBSocket.NoNagleCreateSocket(&sendSocket);
	if(0x00 != nRet)
	{
		nRet = GetLastError();

		strShowInfo.Format(_T("[CCommunication::CheckHeart4PhoneS1] InitializeAndCreateSocket ErrorNo=[%d].\r\n"), nRet);
		XWriteEventLog(strShowInfo);

		m_nExceptionHeart++;
		ttBSocket.CloseSocket( &sendSocket);
		return -2;
	}

	nRet = ttBSocket.ConnectToServer(&sendSocket, CCommunication::m_Port, CCommunication::m_Ip);
	if(0x00 != nRet)
	{	
		Sleep(1000);
		nRet = ttBSocket.ConnectToServer(&sendSocket, CCommunication::m_Port, CCommunication::m_Ip);
		if(nRet != 0x00)
		{
			Sleep(2000);
			nRet = ttBSocket.ConnectToServer(&sendSocket, CCommunication::m_Port, CCommunication::m_Ip);
		}

		if(nRet != 0x00)
		{
			nRet = GetLastError();
			strShowInfo.Format(_T("[CCommunication::CheckHeart4PhoneS1] ConnectToServer IP=[%s] Port =[%d] ErrorNo=[%d].\r\n"), CCommunication::m_Ip,CCommunication::m_Port,nRet);
			XWriteEventLog(strShowInfo);

			m_nExceptionHeart++;
			ttBSocket.CloseSocket( &sendSocket);

			if( 10061 == nRet)
			{
				return -33;
			}
			return -3;
		}
	}
	else
	{
		strShowInfo.Format(_T("[CCommunication::CheckHeart4PhoneS1] ConnectToServer IP=[%s] Port =[%d] Connect OK\r\n"), CCommunication::m_Ip,CCommunication::m_Port);
		XWriteEventLog(strShowInfo);

	}

	//////////////////////////////////////////////////////////////////////
	return  0;

}
开发者ID:niujingqian,项目名称:haocai,代码行数:58,代码来源:Communication.cpp

示例2: SendXmlToPhone

int CCommunication::SendXmlToPhone(CString  argRequest, CString& argResponse,FunctionSocketCallBack CallBack,bool IsSetOutTime)
{
//	argRequest = ReplaceStr( argRequest, _T("&"), _T("&") ); // zhaoshan [2012.01.11]

//  argRequest = ReplaceStr( argRequest, _T("<"), _T("&lt;") ); // zhaoshan [2012.02.27]

	//argRequest = ReplaceStr(argRequest, _T("&"), _T("&amp;") ); // zhaoshan [2012.01.11]

//  argRequest = ReplaceStr( argRequest, _T("\""), _T("&quot;") );

//  argRequest = ReplaceStr( argRequest, _T("\'"), _T("&apos; ") );


	CString FunName=GetRequestFuncationName(argRequest);
	if(CCommunication::m_nExceptionHeart > 0x03)
	{
		XWriteEventLog(_T("[CCommunication::SendXmlToPhone] %s CCommunication::ExceptionHeart > 0x02.\r\n"),FunName);
		return -1;
	}
	//建立连接
	CString  strShowInfo;
	argResponse.Empty();
	CBaseSocket  OclsBSocket ; 
	CBaseSocket* clsBSocket = &OclsBSocket; 
	//CBaseSocket* clsBSocket = CBaseSocket::GetInstance(); 

	if(NULL == clsBSocket) 
	{
		XWriteEventLog(_T("[CCommunication::SendXmlToPhone] CBaseSocket::GetInstance Failed.\r\n"));
		m_nExceptionCounter++;
		return -1;
	}

	int nRet = -1;
	SOCKET sendSocket;
	nRet = clsBSocket->InitializeAndCreateSocket(&sendSocket,IsSetOutTime);
	if(0x00 != nRet)
	{
		nRet = GetLastError();

		strShowInfo.Format(_T("[CCommunication::SendXmlToPhone] InitializeAndCreateSocket ErrorNo=[%d].\r\n"), nRet);
		XWriteEventLog(strShowInfo);

		clsBSocket->CloseSocket( &sendSocket);
		return -2;
	}
	
	nRet = clsBSocket->ConnectToServer(&sendSocket, CCommunication::m_Port, CCommunication::m_Ip);
	if(0x00 != nRet)
	{	
		nRet = GetLastError();
		strShowInfo.Format(_T("[CCommunication::SendXmlToPhone] FunName=[%s] ConnectToServer ErrorNo=[%d].\r\n"),FunName, nRet);
		XWriteEventLog(strShowInfo);

		m_nExceptionCounter++;
		clsBSocket->CloseSocket( &sendSocket);

		if( 10061 == nRet)
		{
			return -33;
		}
		return -3;
	}
	
	//发送命令
	if( ! argRequest.IsEmpty())
	{
		nRet = clsBSocket->SendMsg(&sendSocket, argRequest);
		if(0x00 != nRet)
		{
			int iRevLen=-4;
			nRet = GetLastError();
			::XWriteEventLog(_T("[CCommunication::SendXmlToPhone] %s Request Error=[%d]\n"),FunName,nRet);
			if(nRet == 10060)
				iRevLen=-6;      //Socket 超时返回值        

			clsBSocket->CloseSocket( &sendSocket);
			return iRevLen;
		}
	}else
	{
		XWriteEventLog(_T("[CCommunication::SendXmlToPhone] argRequest is Empty.\r\n"));
		return -5;
	}


	//接收返回值
	argResponse = "";

	clsBSocket->RecvMsg(&sendSocket,argResponse,CallBack);
	return  argResponse.GetLength();
}
开发者ID:niujingqian,项目名称:haocai,代码行数:92,代码来源:Communication.cpp


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