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


C++ CTcpSocket类代码示例

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


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

示例1: doProcess

int doProcess(CTcpSocket &acpsocket)
{
	
	int ret = 0;
    int send_len = 0;
    char recv_buf[4096] = "";
    char send_buf[4096] = "";
	acpsocket.Recv(recv_buf, sizeof(recv_buf), g_para.bank_timeout);
	ret=strlen(recv_buf);
    writelog(LOG_INFO,"len[%d],recv_buf[%s]\n",ret,recv_buf);
    ret = doResponse(recv_buf,strlen(recv_buf),send_buf,send_len);
    if(ret)
    {
        if(ret>0)
            return ret;
    }    
    
    send_buf[send_len]=0;
    writelog(LOG_INFO,"len[%d],send_buf[%s]\n",send_len,send_buf);
	ret = acpsocket.Send(send_buf,send_len);
	if(ret != send_len)
    {
        writelog(LOG_ERR,"发送数据错误\n");
        return -1;
    }
    return 0;
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:27,代码来源:bank_gateway-15.cpp

示例2: GERROR

void CNetBackend::socket_check_cb(ev::timer &w, int32 revents)
{
    if ( EV_ERROR & revents )
    {
        GERROR() << "check socket connect timeout call back error:" << strerror(errno) << "\n";
        w.stop();

        abort_work( "socket_check_cb fail" );
    }

    int64 _now = ev_now(loop);
    CTcpSocket *psocket = null;

    vector<CTcpSocket *>::iterator itr = m_tcp_sockets.begin();
    while ( itr != m_tcp_sockets.end() )
    {
        psocket = *itr;

        itr ++;

        if ( !psocket )  //this fd not use
            continue;

        if ( ES_CONNECTED == psocket->get_socket_status()
             && _now - psocket->get_connect_time() > DISCONNECT_TIMEOUT )
        {
            psocket->close_socket();  //该状态未与游戏服建立连接,可以直接关闭
            psocket->set_socket_status( ES_NONE );
        }
    }
}
开发者ID:changnet,项目名称:game_server,代码行数:31,代码来源:CNetBackend.cpp

示例3: main

int main()
{
	CTcpServer server;
	server.passiveOpen(5566, 10, true);

	char *c = new char[SZ_BUF];
	for (int i = 0; i < SZ_BUF; i++)
		c[i] = (char)0xaa;

	CTcpSocket *conn = server.accept();

	ReadingThread reading(*conn, (char)0x55);
	reading.start();

	unsigned bytecount = 0;
	CTime st = CTime::now();
	while ((CTime::now() - st) < CTime(T_SEND, 0)) {
		bytecount += conn->write(c, SZ_BUF);
	}
	CTime sendtime = CTime::now() - st;
	conn->close();
	cout << "Sent " << bytecount << " bytes." << endl;
	cout << "Sending rate = " << ((double)bytecount * 8 / 1048576 * 1000000 / sendtime.toMicroseconds()) << " Mbps" << endl;

	reading.setDone();
	reading.join();
	CTime recvtime = CTime::now() - reading.tstart();
	cout << "Received " << reading.bytecount() << " bytes." << endl;
	cout << "Receiving rate = " << ((double)reading.bytecount() * 8 / 1048576 * 1000000 / recvtime.toMicroseconds()) << " Mbps" << endl;

	delete [] c;
	return 0;
}
开发者ID:freesamael,项目名称:wolf,代码行数:33,代码来源:testtcpserver.cpp

示例4: CTcpSocket

int	CTcpSocket::DealNewSocket(SOCKET newsock, int connId, char* ip, int port)
{
	CTcpSocket *pNewSocket = new CTcpSocket();
	if (pNewSocket == NULL)
	{
		closesocket(newsock);
		return -1;
	}

	if (pNewSocket->CreateRecvBuf(m_nBufSize) < 0)
	{
		delete pNewSocket;
		closesocket(newsock);
		return -1;
	}

	if (m_pListenSockFunc != NULL)
	{
		m_pListenSockFunc(this, (char*)ip, port, 0, pNewSocket, m_pListenUserData);
	}

	//	变成客户端的连接类
	int ret = pNewSocket->SetSocket(newsock, connId, ip, port, m_pListenSockFunc, m_pListenUserData, (void*)this);
	if (ret < 0)
	{
		if (m_pListenSockFunc != NULL)
		{
			m_pListenSockFunc(this, (char*)ip, port, -1, pNewSocket, m_pListenUserData);
		}
	}

	return 1;
}
开发者ID:crashatom,项目名称:phoebemail,代码行数:33,代码来源:TcpSocket.cpp

示例5: on_accept_callback

	void CTcpSocket::on_accept_callback(evutil_socket_t sock, short flag, void* p)
	{
		CTcpSocket* psocket = (CTcpSocket*)(p);
		if (psocket && (flag&EV_READ))
		{
			psocket->on_inter_listen_accept(sock);
		}
	}
开发者ID:mybestcool,项目名称:xhnet,代码行数:8,代码来源:impl_tcpsocket.cpp

示例6: on_send_callback

	void CTcpSocket::on_send_callback(evutil_socket_t sock, short flag, void* p)
	{
		CTcpSocket* psocket = (CTcpSocket*)(p);
		if (psocket)
		{
			psocket->on_inter_send(flag);
		}
	}
开发者ID:mybestcool,项目名称:xhnet,代码行数:8,代码来源:impl_tcpsocket.cpp

示例7: Create

	CTcpSocket* CTcpSocket::Create(void)
	{
		CTcpSocket* socket = CTcpSocket::m_pool.New_Object();
		if (socket)
		{
			socket->Set_DestoryCB([](CPPRef* ref){ CTcpSocket::m_pool.Delete_Object(dynamic_cast<CTcpSocket*>(ref)); });
		}

		return socket;
	}
开发者ID:mybestcool,项目名称:xhnet,代码行数:10,代码来源:impl_tcpsocket.cpp

示例8: 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

示例9: ThreadFunc

DWORD WINAPI CTcpSocket::ThreadFunc(LPVOID pParam)
{
	DWORD dwWaitStatus;						// holds return value of the wait
	CTcpSocket * p = (CTcpSocket *)pParam;	// connection-specific data

	while (TRUE) 
	{
		// Wait for an event 
		dwWaitStatus = WSAWaitForMultipleEvents(3, p->m_eventArray,
												FALSE,        // fWaitAll
												WSA_INFINITE, // dwTimeout
												FALSE);        // fAlertable
		switch (dwWaitStatus) 
		{
		case WSA_WAIT_FAILED:	// A fatal error
			OutputDebugString(_T("WSAWaitForMultipleEvents() failed\n"));
			break;

		case WSA_WAIT_EVENT_0:	// The hNetEvent has been signaled.
			if (p->HandleNetworkEvent())
			{
				continue;
			}
			break;

		case WSA_WAIT_EVENT_0+1:	// The hOutputEvent has been signaled.
			if (p->HandleOutputEvent())
			{
				continue;
			}
			break;

		case WSA_WAIT_EVENT_0+2:	// Quit time;
			OutputDebugString(_T("Hangup by myself\n"));
			break;

		default:
			OutputDebugString(_T("Unknown error in WSAWaitForMultipleEvents()\n"));
			break;
		} // switch (dwWaitStatus)
		// Break out of the while loop.
		break;
	} // while (1)

	// Thread is ending because the connection was closed or an error occurred
	p->CleanUp();
	return 0;
}
开发者ID:AlexVangelov,项目名称:ar168l,代码行数:48,代码来源:TcpSocket.cpp

示例10: doProcess

int doProcess(CTcpSocket &acpsocket)
{
    printf("\n doProcess3  \n");
	
	
	int ret = 0;
    int pack_len = 0;
    int send_len = 0;
    char recv_buf[4096] = "";
    char send_buf[4096] = "";
	//ret = acpsocket.Recv(recv_buf, HEAD_LEN, g_para.bank_timeout);
    //ret = acpsocket.Recv(recv_buf, 2048, g_para.bank_timeout);
	//ret = acpsocket.Recv(recv_buf, sizeof(recv_buf), g_para.bank_timeout);
	acpsocket.Recv(recv_buf, sizeof(recv_buf), g_para.bank_timeout);
	ret=strlen(recv_buf);
	printf("\n recv_buf[%s] \n len[%d] ret[%d]\n",recv_buf,strlen(recv_buf),ret);
// 	if(ret != HEAD_LEN)
//     {
//         writelog(LOG_ERR,"接收银行数据包头错误\n");
//         return -1;
//     }
//     pack_len = atoi(recv_buf);
//     ret = acpsocket.Recv(recv_buf, pack_len, g_para.bank_timeout);
//     if(ret != pack_len)
//     {
//         writelog(LOG_ERR,"接收银行数据包体错误\n");
//         return -1;
//     }
 //   recv_buf[pack_len]=0;
    writelog(LOG_INFO,"len[%d],recv_buf[%s]\n",ret,recv_buf);
    ret = doResponse(recv_buf,pack_len,send_buf,send_len);
    if(ret)
    {
        if(ret>0)
            return ret;
    }    
    
    send_buf[send_len]=0;
    writelog(LOG_INFO,"len[%d],send_buf[%s]\n",send_len,send_buf);
    ret = acpsocket.Send(send_buf,send_len);
    if(ret != send_len)
    {
        writelog(LOG_ERR,"发送数据错误\n");
        return -1;
    }
    return 0;
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:47,代码来源:bank_gateway-2.cpp

示例11: sendQueryIPByName_DDNS

bool sendQueryIPByName_DDNS(CDvrDevice* device, char *name, int namelen)
{
	unsigned char data[HEADER_SIZE + 64];
	memset(data, 0, HEADER_SIZE + 64);
	
	data[0] = 0x81;
	DWORD datalen = (namelen <= 64) ? namelen : 64;
	(*(DWORD*)(data+4)) = datalen;//datalen;
	data[8] = 50;
	data[9] = 0; //0.单记录查询  1.查询所有在线记录
    memcpy(data+HEADER_SIZE, name, datalen);
	
    CTcpSocket *tcp = device->GetTcpSocket();
    if (tcp)
    {
        if (tcp->WriteData((char*)data, HEADER_SIZE + 64) >= 0)
        {
            return true;
        }
    }
    return false;
}
开发者ID:crashatom,项目名称:phoebemail,代码行数:22,代码来源:dvrpacket_DDNS.cpp

示例12: doProcess

int doProcess(CTcpSocket &acpsocket)
{
    int ret = 0;
    int pack_len = 0;
    int send_len = 0;
    char recv_buf[256] = "";
    char send_buf[256] = "";
    ret = acpsocket.Recv(recv_buf, HEAD_LEN, g_para.bank_timeout);
    if(ret != HEAD_LEN)
    {
        writelog(LOG_ERR,"接收银行数据包头错误\n");
        return -1;
    }
    pack_len = atoi(recv_buf);
    ret = acpsocket.Recv(recv_buf, pack_len, g_para.bank_timeout);
    if(ret != pack_len)
    {
        writelog(LOG_ERR,"接收银行数据包体错误\n");
        return -1;
    }
	recv_buf[pack_len]=0;
    writelog(LOG_INFO,"len[%d],recv_buf[%s]\n",ret,recv_buf);
    ret = doResponse(recv_buf,pack_len,send_buf,send_len);
    if(ret)
    {
        if(ret>0)
            return ret;
    }    
    
    send_buf[send_len]=0;
    writelog(LOG_INFO,"len[%d],send_buf[%s]\n",strlen(send_buf),send_buf);
    ret = acpsocket.Send(send_buf,send_len);
    if(ret != send_len)
    {
        writelog(LOG_ERR,"发送数据错误\n");
        return -1;
    }
    return 0;
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:39,代码来源:bank_gateway.cpp

示例13: 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

示例14: Accept

bool CTcpSocket::Accept(CTcpSocket &sock, char *remote)
{
    int tempsocket;
    struct sockaddr_in addr;
    addr.sin_family=m_nAddressFamily;
    SOCKADDRLEN addrlen = sizeof(addr);
    tempsocket = accept( m_socket, (struct sockaddr*)&addr, &addrlen);
    if(tempsocket < 0)
    {
        return false;
    }
    sock.Attach(tempsocket,m_nAddressFamily);
    if (remote!=NULL)
        strcpy(remote,inet_ntoa(addr.sin_addr));
    return(true);
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:16,代码来源:tcp.cpp

示例15: send_to_bank

static int send_to_bank(const std::string& req, std::string& resp)
{
    int timeout = 0;
    string ytc_ip;
    int ytc_port;
    int ret;

	ret = get_ec_para(YTCEC_TIMEOUT, timeout);
	if (ret)
    {
        LOG(ERROR, "中银通超时时限未配置");
        return E_COMMON_ERR;
    }

	timeout*=1000;

    ret = get_ec_para(YTCEC_SVRIP, ytc_ip);
    if (ret)
    {
        LOG(ERROR, "中银通前置机未配置");
        return E_COMMON_ERR;
    }
    ret = get_ec_para(YTCEC_SVRPORT, ytc_port);
    if (ret)
    {
        LOG(ERROR, "中银通前置机未配置");
        return E_COMMON_ERR;
    }
    LOG(DEBUG, "中银通服务器[" << ytc_ip << ":" << ytc_port << "]");

    CTcpSocket sock;
    alarm(timeout / 1000);
    if (!sock.ConnectTcp((char*)ytc_ip.c_str(), ytc_port))
    {
        alarm(0);
        LOG(ERROR, "connect to ytc error");
        return E_COMMON_ERR;
    }
    alarm(0);

    // 中银通报文
    stringstream ss;
    ss << setw(4) << setfill('0') <<  req.length() << req;

    LOG(DEBUG, "中银通报文[" << ss.str() << "]");
    if (sock.Send((char*)ss.str().c_str(), ss.str().length()) < (int)ss.str().length())
    {
        LOG(ERROR, "Send to ytc error");
        return E_COMMON_ERR;
    }
    // 包头
    size_t body_len = 0;
    {
        char buffer[5] = {0};
        if (sock.Recv(buffer, 4, timeout) <= 0)
        {
            LOG(ERROR, "Recv body head from ykc error");
            return E_COMMON_ERR;
        }
        istringstream is(buffer);
        is >> body_len;
    }
    // 包体
    string body;
    {
        char* buffer = new char[body_len + 1];
        memset(buffer, 0, body_len + 1);
        if (sock.Recv(buffer, body_len, timeout) <= 0)
        {
            LOG(ERROR, "Recv body from ykc error");
            delete [] buffer;
            return E_COMMON_ERR;
        }
        body = buffer;
        delete [] buffer;

        resp = body;
    }
    return 0;
}
开发者ID:pennwin2014,项目名称:bank_interface,代码行数:80,代码来源:F3404.cpp


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