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


C++ CTcpSocket::Close方法代码示例

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


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

示例1: main

int main()
{
	bool flag = false;
	int childpid = 0;
	char *inifile = "transconfig.ini";
	CTcpSocket tcpsocket ;
	CTcpSocket sockettmp ;
	
	openlog("yktbank_gateway",LOG_PID|LOG_CONS|LOG_NDELAY,LOG_LOCAL3);
	
	if( !ReadIni(inifile))
	{
	    printf("读取配置文件[%s]失败\n",inifile);
	    return -1;
	}
	
	writelog(LOG_DEBUG,"drtp_ip[%s],drtp_port[%d],mainfuncno[%d],list_port[%d]",g_para.drtp_ip, g_para.drtp_port, g_para.main_funcno,g_para.bank_lisnport);
	printf("\n drtp_ip[%s],drtp_port[%d],mainfuncno[%d],list_port[%d]\n",g_para.drtp_ip, g_para.drtp_port, g_para.main_funcno,g_para.bank_lisnport);
	if(!KS_YKT_Clt::Initialize(g_para.drtp_ip, g_para.drtp_port, g_para.main_funcno))
	{
		printf("----------初始化通讯平台失败----------\n");
		return 	-1;
	}
		
	signal(SIGCHLD,SIG_IGN);
	signal(SIGINT,SIG_DFL);
	flag = tcpsocket.Create();
	if(!flag)
		exit(1);
	flag = tcpsocket.Listen(g_para.bank_lisnport);
	if(!flag)
		exit(1);
	while(1)
	{
			flag = tcpsocket.Accept(sockettmp);
			
			if(!flag)
				continue;
			if ((childpid = fork()) == 0)		/* 子进程 */
			{   
				tcpsocket.Close();  
				
				doProcess(sockettmp);     /* 处理请求 */  
				
				sockettmp.Close();
				exit(0);  
			}  
			else if(childpid < 0)
			{
                		printf("创建子进程失败\n");
			}
			sockettmp.Close(); 		/* 父进程 */  
	}
	tcpsocket.Close();
	KS_YKT_Clt::Uninitialize();
	closelog();
	return 0;
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:58,代码来源:bank_gateway-15.cpp

示例2: Accept

bool CTcpSocket::Accept(CTcpSocket& nNew)
{
    nNew.Close();
    
    struct sockaddr_in nInAddress;
    socklen_t nSize = sizeof(nInAddress);
    int fd = accept(m_nFd, (struct sockaddr*)&nInAddress, &nSize);
    if (fd == -1)
        return false;
    
    nNew.m_nFd = fd;
    nNew.m_strHost = inet_ntoa(nInAddress.sin_addr);
    nNew.m_nPort = (unsigned int)ntohs(nInAddress.sin_port);
    return true;
}
开发者ID:xyz13,项目名称:tcpproxy,代码行数:15,代码来源:tcpsocket.cpp

示例3: F1005

int F1005(TRUSERID *handle,int iRequest,ST_PACK *in_pack,int *pRetCode,char *szMsg)
{
	
	*pRetCode=-1;
	ST_CPACK aPack;	
	
	CCcbTrans   CcbTran(in_pack);
	CCcbTrans *pTrans = &CcbTran;
	if(!pTrans->AssignReqHead())
	{
		strcpy(szMsg,pTrans->getErrMsg().c_str());
	    	cout<<szMsg<<endl;
		return -1;
	}	
	pTrans->prepare();
	if(!pTrans->replaceTemlateVarValue())
	{
		strcpy(szMsg,pTrans->getErrMsg().c_str());
	    	cout<<szMsg<<endl;
		return -1;
	}

	CTcpSocket  tcpSocket;
	int ret = -1;
	string bankReqData,bankRepData;

	if(!pTrans->buildBankRequestData(pTrans->getTemplateData(),bankReqData))
	{
		strcpy(szMsg,pTrans->getErrMsg().c_str());
	    	cout<<szMsg<<endl;
		return -1;
	}

	if(pTrans->debug)
		cout<<"bankReqData : "<<bankReqData<<endl;

	if(!tcpSocket.ConnectTcp((char*)pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port))
	{
		*pRetCode=9999;
		sprintf(szMsg,"连接建设银行IP[%s]Port[%d] 失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
	    	cout<<szMsg<<endl;
		writelog(LOG_ERR,szMsg);
		return -1;
	}
	ret = tcpSocket.Send((char*)bankReqData.c_str(),bankReqData.length());
	if(ret!=bankReqData.length())
	{
		*pRetCode=9999;
		sprintf(szMsg,"发送数据到建设银行IP[%s]Port[%d] 失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
	    	cout<<szMsg<<endl;
		writelog(LOG_ERR,szMsg);
		return -1;
	}

	CCBMSGHEAD ccbMsgHead;
	char *pccbMsgHead =(char*) &ccbMsgHead;
	ret = tcpSocket.Recv(pccbMsgHead, sizeof(ccbMsgHead), pTrans->pBank->getNCService().timeout*1000);		
	if(ret<=0)
	{
		char errBuf[256];
		*pRetCode=9999;
		tcpSocket.GetLastError(errBuf);
		sprintf(szMsg,"从建设银行IP[%s]Port[%d] 接收数据失败,Error: %s\n",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port,errBuf);
	    	cout<<szMsg<<endl;
		writelog(LOG_ERR,szMsg);
		return -1;
	}

	if(pTrans->debug)
	{
		cout<<"length: "<< ccbMsgHead.msgDataLen<<"type: " <<ccbMsgHead.commMode<<endl;
		cout<<ccbMsgHead.txCode <<" curblock : "<<ccbMsgHead.curBlock<<"totalblock : "<<ccbMsgHead.totalBlock<<endl;
	}
	int len = atoi(ccbMsgHead.msgDataLen);
	char *pRcvBuf=(char*)malloc(len+1);
	
	ret = tcpSocket.Recv(pRcvBuf, len, pTrans->pBank->getNCService().timeout*1000);		
	if(ret<=0)
	{
		*pRetCode=9999;
		sprintf(szMsg,"从建设银行IP[%s]Port[%d] 接收数据失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
	    	cout<<szMsg<<endl;
		writelog(LOG_ERR,szMsg);
		return -1;
	}
	
	tcpSocket.Close();
	pRcvBuf[len] =0;
	bankRepData = string(pRcvBuf,len);
	if(pTrans->debug)
		cout<<"报文: bankRepData:"<<bankRepData<<endl;

	free(pRcvBuf);

	string fileName;
	if(getNodeValByNodeName(bankRepData, "FileName", fileName))
	{
		string repFile = "ccbRepFile";
		fileName.erase(0,fileName.find('\\'));
		repFile += fileName;
//.........这里部分代码省略.........
开发者ID:Codiscope-Research,项目名称:ykt4sungard,代码行数:101,代码来源:F1005.cpp

示例4: F1004

int F1004(TRUSERID *handle,int iRequest,ST_PACK *in_pack,int *pRetCode,char *szMsg)
{
    int ret=0;

    *pRetCode=-1;
    ST_CPACK aPack;
    CTcpSocket  tcpSocket;
    CBcomTrans   BcomTran(in_pack);
    CTrans *pTrans = &BcomTran;
    if(!pTrans->AssignReqHead())
    {
        strcpy(szMsg,pTrans->getErrMsg().c_str());
        cout<<szMsg<<endl;
        return -1;
    }
    pTrans->prepare();

    if(!pTrans->replaceTemlateVarValue())
    {
        strcpy(szMsg,pTrans->getErrMsg().c_str());
        cout<<szMsg<<endl;
        return -1;
    }

    string signReqData;
    if(!pTrans->buildSignData(signReqData))
    {
        strcpy(szMsg,pTrans->getErrMsg().c_str());
        cout<<szMsg<<endl;
        return -1;
    }
    if(pTrans->debug)
        cout<<"signReqData: "<<signReqData.c_str()<<endl;


    if(!tcpSocket.ConnectTcp((char*)pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port))
    {
        *pRetCode=9999;
        sprintf(szMsg,"连接交通银行IP[%s]Port[%d] 失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
        cout<<szMsg<<endl;
        writelog(LOG_ERR,szMsg);
        return -1;
    }
    ret = tcpSocket.Send((char*)signReqData.c_str(),signReqData.length());
    if(ret!=signReqData.length())
    {
        *pRetCode=9999;
        sprintf(szMsg,"发送数据到交通银行IP[%s]Port[%d] 失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
        cout<<szMsg<<endl;
        writelog(LOG_ERR,szMsg);
        return -1;
    }

    char szBufLen[8]="";
    int timout=pTrans->pBank->getNCService().timeout*1000;
    ret = tcpSocket.Recv(szBufLen, 7, timout);
    if(ret<=0)
    {
        *pRetCode=9999;
        sprintf(szMsg,"从交通银行IP[%s]Port[%d] 接收数据失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
        cout<<szMsg<<endl;
        writelog(LOG_ERR,szMsg);
        return -1;
    }
    if(pTrans->debug)
        cout<<"报文长度 : "<<szBufLen<<endl;
    int len = atol(szBufLen+1);

    char *pRcvBuf=(char*)malloc(len+1);
    if(!pRcvBuf)
    {
        *pRetCode=9999;
        sprintf(szMsg,"从交通银行IP[%s]Port[%d] 接收数据失败",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port);
        cout<<szMsg<<endl;
        writelog(LOG_ERR,szMsg);
        free(pRcvBuf);
        return -1;
    }
    ret = tcpSocket.Recv(pRcvBuf, len, timout);
    if(ret<0)
    {
        char errBuf[256];
        *pRetCode=9999;
        tcpSocket.GetLastError(errBuf);
        sprintf(szMsg,"从交通银行IP[%s]Port[%d] 接收数据失败,errMsg:[%s]",pTrans->pBank->getNCService().ip.c_str(),pTrans->pBank->getNCService().port,errBuf);
        cout<<szMsg<<endl;
        writelog(LOG_ERR,szMsg);
        free(pRcvBuf);
        return -1;
    }
    tcpSocket.Close();
    pRcvBuf[len] = 0;
    if(pTrans->debug)
        cout<<pRcvBuf<<endl;

    string bankVerifyData(pRcvBuf,len);
    free(pRcvBuf);

    string bankData;
    if(!pTrans->parseVerifyBankSignedData(bankVerifyData,bankData))
//.........这里部分代码省略.........
开发者ID:nykma,项目名称:ykt4sungard,代码行数:101,代码来源:F1004.cpp

示例5: Bank_Trans

//按照通用接口规范进行封装各自银行的接口
int Bank_Trans(TRUSERID *handle,ST_PACK *rPack,int *pRetCode,char *szMsg)
{
	int ret=0;

	char host[16]="";		//对方机器的ip
	short port = 0;			
	int timeout = 0;

	ST_CPACK aPack;	
	ST_PACK *out_pack = &(aPack.pack);
	CTcpSocket tcpsocket ;
//	TRANS_REQ trans_req;
//	TRANS_ANS trans_ans;
//	memset(&trans_req,0x20,sizeof trans_req);			// 先填充上空格
//	memset(&trans_ans,0x20,sizeof trans_ans);

	ResetNormalCPack(&aPack,0,1);
	SetCol(handle,0);
	SetCol(handle,F_LVOL1,F_SDATE1,F_SCERT_NO,0);
	char buf[256] = " ";
	char tmp[8] = "";
	
	des2src(host,rPack->scust_auth);					// 银行ip
	port=rPack->lvol0;									// 银行端口
	timeout = rPack->lvol1;								// 等待银行超时时间

	memset(buf,0x20,sizeof buf);			// 填充空格
	sprintf(buf,"%04d",REQ_PACK_LEN);		// 包体长度
	strcpy(buf+4,TRANS_CODE);				// 交易码
	STRLENCPY(buf+8, rPack->sdate0,8);				// 交易日期
	STRLENCPY(buf+16, rPack->sholder_ac_no,14);		// 一卡通交易参考号
	STRLENCPY(buf+30, rPack->sphone,32);				// 学工号
	STRLENCPY(buf+62, rPack->scust_limit,32);			// 姓名
	STRLENCPY(buf+94,rPack->scust_auth2,18);			// 身份证号
	STRLENCPY(buf+112,rPack->scust_limit2,32);		// 银行卡号
	sprintf(buf+144,"%-17.2f",rPack->damt0);			// 交易金额
	
	ret = tcpsocket.ConnectTcp(host, port);
	if(!ret)
	{
		writelog(LOG_ERR,"Connect to bank error,error code is [%d],ip_addr[%s],port[%d] ",ret,host,port);
		return E_TRANS_CONNECT;
	}

	buf[REQ_PACK_LEN+4]=0;
	writelog(LOG_INFO,"trans send[%s]",buf);
	
	ret = tcpsocket.Send(buf,REQ_PACK_LEN+4);
	if (ret<0)
	{
	      	// 发送时,端口有问题,包括被关闭
		writelog(LOG_ERR,"Send to bank error,error code is [%d] ",ret);
		return E_TRANS_REQUEST;
	}
	else if(0==ret){
		// 不应该出现这种情况
	      	writelog(LOG_ERR,"Send to bank,CTcpSocket.Send should not return this error,error code is[%d]!\n",ret);
		return E_TRANS_REQUEST;
	}

	ret = tcpsocket.Recv(buf, ANS_PACK_LEN+4, timeout);		
	tcpsocket.Close();
	
	buf[ANS_PACK_LEN+4]=0;
	writelog(LOG_INFO,"trans recv[%s],recvlen[%d]",buf,ret);

	if (ret!=ANS_PACK_LEN+4)
	{
		out_pack->lvol1 = -1;							//超时 或 接收数据错误 ,后台应向银行发冲正
		strcpy(szMsg,"接收银行数据错误");
		goto r_ret;  
	}

	memcpy(tmp,buf,4);
	if(atoi(tmp) != ANS_PACK_LEN)
	{
		out_pack->lvol1 = -1;							//接收数据错误,后台应向银行发冲正
		strcpy(szMsg,"接收银行数据错误");
		goto r_ret;  // 接收时候出错,或超时
	}
	memcpy(tmp,buf+20,6);								
	if(strncmp(tmp,"000000",6)!=0)    // 银行端处理错误
	{
		memcpy(szMsg,buf+26,34);
		return E_TRANS_BANK_RECVERR;
	}
	else
	{
		out_pack->lvol1 = 1;
		memcpy(out_pack->sdate1,buf+12,8);						// 银行流水号
		memcpy(out_pack->scert_no,buf+26,34);					// 银行返回信息
	}
	writelog(LOG_INFO,"bankretinfo[%s],lvol1[%d]",out_pack->scert_no,out_pack->lvol1);
r_ret:
	PutRow(handle,out_pack,pRetCode,szMsg);
	return 0;
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:98,代码来源:bank_ccb_nm.cpp

示例6: F250001

int F250001(TRUSERID *handle,int iRequest,ST_PACK *rPack,int *pRetCode,char *szMsg)
{

	int ret=0;

	char host[16]="";		//对方机器的ip
	short port=0;			
	ST_CPACK aPack;
	ST_PACK *out_pack = &(aPack.pack);
	//char senddata[327]="";
	//char rcvdata[327]="";
	CTcpSocket tcpsocket ;
	ext_phone_trans_t  PhoneTrans;

	
	//memset(senddata,0x20,sizeof senddata);
	//memset(rcvdata,0x20,sizeof rcvdata);

	ResetNormalCPack(&aPack,0,1);
	SetCol(handle,0);
	SetCol(handle,F_SSECTYPES,0);

	des2src(host,rPack->scust_auth);
	port=rPack->lvol0;
	memset(&PhoneTrans,0,sizeof(PhoneTrans));
	memcpy(&PhoneTrans, rPack->ssectypes,127);
	memset(PhoneTrans.plugdata,0x20,sizeof(PhoneTrans.plugdata));
	memcpy(PhoneTrans.signcheck,gSignCheck,strlen(gSignCheck));
	
	//writelog(LOG_ERR,"package_send=[%s] ",rPack->ssectypes);
	writelog(LOG_INFO,"Connect to boss system ip_addr[%s],port[%d] ",host,port);	
	ret = tcpsocket.ConnectTcp(host, port);
	if(!ret)
	{
		writelog(LOG_ERR,"Connect to boss system error,error code is [%d],ip_addr[%s],port[%d] ",ret,host,port);
		*pRetCode=E_MOBILE_TRANS_CONNECT;
		goto l_ret;
	}
	printf("send:[%s]\n",(char*)&PhoneTrans);
	ret = tcpsocket.Send((char*)&PhoneTrans,sizeof(PhoneTrans));
	if (ret<0)
	{
	      	// 发送时,端口有问题,包括被关闭
		writelog(LOG_ERR,"Send to boss system error,error code is [%d] ",ret);
		*pRetCode=E_MOBILE_TRANS_REQUEST;
		goto l_ret;
	}
	else if(0==ret){
		// 不应该出现这种情况
	      	writelog(LOG_ERR,"Send to boss system error,CTcpSocket.Send should not return this error,error code is[%d]!\n",ret);
		*pRetCode=E_MOBILE_TRANS_REQUEST;
	      	goto l_ret;
	}
	memset(&PhoneTrans,0,sizeof(PhoneTrans));
	if(rPack->lvol1==1)
	{
		ret = tcpsocket.Recv((char*)&PhoneTrans, sizeof(PhoneTrans), 15000);		
		tcpsocket.Close();
		if (ret!=327)
		{
			writelog(LOG_ERR,"Receive from boss system error,error code is [%d] ",ret);
			*pRetCode=E_MOBILE_TRANS_TIME_OUT;
			goto l_ret;  // 接收时候出错,或超时
		}
		printf("trans recv:[%s]\n",(char*)&PhoneTrans);
//		memcpy(out_pack->ssectypes,&PhoneTrans,127);
		if(strncmp(PhoneTrans.retnum,"00",2)!=0)
		{
			if(strncmp(PhoneTrans.retnum,"01",2)==0)
			{
				writelog(LOG_ERR,"trans error,mac error,ret[%s] ",PhoneTrans.retnum);
				*pRetCode=E_MOBILE_CHECK_MAC;
				goto l_ret;
			}
			if(strncmp(PhoneTrans.retnum,"02",2)==0)
			{
				writelog(LOG_ERR,"trans falied ret[%s] ",PhoneTrans.retnum);

				*pRetCode=E_MOBILE_TRANS_FAILURE;
				goto l_ret;
			}
		}
		else
		{
			writelog(LOG_INFO,"trans success ret[%s] ","00");
		}
	}else{
		ret=tcpsocket.Recv((char*)&PhoneTrans, sizeof(PhoneTrans), 15000);
		tcpsocket.Close();
		if (ret!=327)
		{
			writelog(LOG_ERR,"Receive from boss system error,error code is [%d] ",ret);
			*pRetCode=E_MOBILE_TRANS_TIME_OUT;
			goto l_ret;  // 接收时候出错,或超时
		}
		printf("query recv:[%s]\n",(char*)&PhoneTrans);	
//		memcpy(out_pack->ssectypes,&PhoneTrans,127);
		if(strncmp(PhoneTrans.retnum,"00",2)!=0)
		{
			if(strncmp(PhoneTrans.retnum,"01",2)==0)
//.........这里部分代码省略.........
开发者ID:nykma,项目名称:ykt4sungard,代码行数:101,代码来源:F250001.cpp

示例7: OpenSocketPair

bool CTcpSocket::OpenSocketPair(CTcpSocket& nIn, CTcpSocket& nOut)
{
#if defined(WIN32)
    CTcpSocket nServer;
    nServer.SetAddress("127.0.0.1", 0);    
    if (!nServer.Open())
    {
        AILOG_ERROR() << "OpenSocketPair[Open] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    
    if (!nServer.Listen())
    {
        AILOG_ERROR() << "OpenSocketPair[Listen] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    
    std::string strHost;
    int nPort;
    if (!nServer.GetLocalAddress(strHost, nPort))
    {
        AILOG_ERROR() << "OpenSocketPair[GetLocalAddress] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    nIn.SetAddress("127.0.0.1", nPort);
    if (!nIn.Open())
    {
        AILOG_ERROR() << "OpenSocketPair[Open client] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    nIn.SetOptNB(true);
    if (!nIn.Connect())
    {
        if (!CTcpSocket::IsNoneFatal())
        {
            nIn.Close();
            AILOG_ERROR() << "OpenSocketPair[Connect] error," << CTcpSocket::LastErrorMessage();
            return false;
        }
    }

    if (!nServer.Accept(nOut))
    {
        nIn.Close();
        AILOG_ERROR() << "OpenSocketPair[Accept] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    nIn.SetOptNB(false);
    return true;
#else
    int fds[2];
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0)
    {
        AILOG_ERROR() << "OpenSocketPair[socketpair] error," << CTcpSocket::LastErrorMessage();
        return false;
    }
    
    nIn.m_nFd = fds[0];
    nOut.m_nFd = fds[1];
    return true;
#endif
}
开发者ID:xyz13,项目名称:tcpproxy,代码行数:62,代码来源:tcpsocket.cpp


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