本文整理汇总了C++中CTcpSocket::Create方法的典型用法代码示例。如果您正苦于以下问题:C++ CTcpSocket::Create方法的具体用法?C++ CTcpSocket::Create怎么用?C++ CTcpSocket::Create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CTcpSocket
的用法示例。
在下文中一共展示了CTcpSocket::Create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例2: send_to_and_recv_from_bank_of_boc
int send_to_and_recv_from_bank_of_boc(char* pSend, int send_len,char* pRecv,int *recv_len,int max_recv_len,int delay_time)
{
struct SockNode SockNode,SockNode_rsp;
CTcpSocket sock;
char bank_ip[20]="";
int bank_port=0;
int bank_port_rsp=0;
//int delay_time=0;
int ret=0;
char temp[5]="";
int length=0;
TIniFile tf;
if (!tf.Open(TRANS_INI_FILE))
{
writelog(LOG_ERR,"Cann't open ini file,file=[%s]\n",TRANS_INI_FILE);
return E_TRANS_UNKNOW_ERROR;
}
tf.ReadString("BANK", "BANK_IP", "127.0.0.1", bank_ip,sizeof(bank_ip));
bank_port=tf.ReadInt("BANK", "BANK_PORT",9001);
bank_port_rsp=tf.ReadInt("BANK", "BANK_PORT_RSP",9002);
if(0==delay_time) delay_time=tf.ReadInt("BANK", "BANK_DELAY_TIME", 3000);
tf.Close();
#ifdef DEBUG
//printf("BANK_IP=[%s],BANK_PORT[%d],DELAY_TIME[%d]\n",bank_ip,bank_port,delay_time);
#endif
writelog(LOG_DEBUG,"connect to bank [%s][%d][%d]",bank_ip,bank_port,bank_port_rsp);
//连接银行
ret=SockNode.hSock.ConnectTcp(bank_ip, bank_port,5);
if(!ret)
{
writelog(LOG_ERR,"Connect to bank error,error code is [%d],bank_ip[%s],port[%d] ",ret,bank_ip,bank_port);
return E_TRANS_BANK_NETERR;
}
//发送数据到银行
ret=SockNode.hSock.Send(pSend, send_len);
if (ret<0)
{
// 发送时,端口有问题,包括被关闭
writelog(LOG_ERR,"Send to bank error,ret = %ret, error code is [%d] ",ret, errno);
return E_TRANS_BANK_NETERR;
}
else if (0==ret)
{
// 不应该出现这种情况
writelog(LOG_ERR,"Send to bank error,CTcpSocket.Send should not return this error,error code is[%d]!\n",errno);
return E_TRANS_BANK_NETERR;
}
writelog(LOG_DEBUG,"send to bank success, waiting for responsing!!");
sleep(1);
if(!sock.Create())
{
writelog(LOG_ERR,"sock.Create error!\n");
return E_TRANS_BANK_NETERR;
}
if(!sock.Listen(bank_port_rsp))
{
writelog(LOG_ERR,"sock.Listen error![%d]",errno);
return E_TRANS_BANK_NETERR;
}
CTcpSocket rcv_sock;
if(sock.Accept(rcv_sock))
{
int rcvlen = rcv_sock.Recv(pRecv,max_recv_len,delay_time);
if(rcvlen < 0)
{
writelog(LOG_ERR,"sock.Recv error![%d][%d]\n",rcvlen,errno);
return E_TRANS_BANK_NOANSWER;
}
writelog(LOG_DEBUG,"recv length[%d]",rcvlen);
*recv_len=rcvlen;
return 0;
}
return E_TRANS_BANK_NOANSWER;
/*
ret=SockNode_rsp.hSock.Listen(bank_port_rsp);
if(!ret)
{
char msg[256]="";
SockNode_rsp.hSock.GetLastError(msg);
writelog(LOG_ERR,"Listen bank error,error code is[%d],msg=[%s]!\n",ret,msg);
return E_TRANS_BANK_NETERR;
}
CTcpSocket rcv_sock;
ret=SockNode_rsp.hSock.Accept(rcv_sock);
if(ret)
{
int rcvlen = SockNode.hSock.Recv(pRecv,max_recv_len,delay_time);
if(rcvlen < 0)
{
char msg[256]="";
//.........这里部分代码省略.........