예제 #1
0
int Rusock()
{
    unlink("./USOCK");
    CSocket sock;
    nRet = sock.Listen("./USOCK");
    if (nRet < 0)
    {
        printf("Listen failed:%s \n", strerror(errno));
        return -1;
    }

    int fd = sock.Accept();
    if (nRet < 0)
    {
        printf("Accept failed:%s \n", strerror(errno));
        return -1;
    }

    while (true)
    {
        if (_HandleSignal() < 0)
            break;

        gettimeofday(&time_a, NULL);
        nRet = read(fd, rbuff, g_len);
        gettimeofday(&time_b, NULL);
        struct timeval *time_w = (struct timeval *)(rbuff);

        if (nRet != g_len)
        {
            printf("read fialed nRet %d %s\n", nRet, strerror(errno));
            break;
        }
        g_count0++;

        if(g_verify == 1)
        {
            struct data* d = (struct data* )(rbuff+sizeof(struct timeval));
            if( d->compare() < 0)
            {
                printf("format error %s\n");fflush(NULL);
            }
        }

        unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec);
        total_time0 += use_time0;

        unsigned long long delay_time = (time_b.tv_sec - time_w->tv_sec) * 1000000 + (time_b.tv_usec - time_w->tv_usec);
        if(delay_time < 10000)
        {
            if(delay_time > g_delay_max)
                g_delay_max = delay_time;
            recv_delay_count0++;
            recv_delay0 += delay_time;
        }
    }

    return 0;
}
예제 #2
0
void CServer::Run()
{
	Init();

	CSocket sock;
	if(!sock.Socket()) return;
	if(!sock.Bind( CConfig::GetInstance()->GetPortPID(ID_SERVER), CConfig::GetInstance()->GetAddrPID(ID_SERVER)) ) return;
	
	cout << "listening.." << endl;
	sock.Listen();

	
	CWireThread* pThreadWire = new CWireThread();
	pThreadWire->Start();

	CGateThread* pThreadGate = new CGateThread();
	pThreadGate->Start();
	 
	if(!sock.Accept(m_sockMain)) return;
	if(!sock.Accept(m_sockOT)) return;
	sock.Close();

	cout << "accepted sockets!" << endl;

	COTThread* pThreadOT = new COTThread();

	pThreadOT->Start();
 
	RunMainThread();
	m_sockMain.Close();

	pThreadOT->Wait();
	m_sockOT.Close();

	pThreadGate->Wait();
	pThreadWire->Wait();

	Cleanup();

	delete pThreadOT;
	delete pThreadGate;
	delete pThreadWire;
	
} 
예제 #3
0
bool CServerWorkFlow::Accept(SOCKET hListenSock)
{
	CSocket socket;
	SOCKET hOldSock = socket.Detach();
	socket.Attach(hListenSock);
	BOOL bAccept = socket.Accept(*m_pServerSocket);
	socket.Detach();
	socket.Attach(hOldSock);
	return (TRUE == bAccept);
}
예제 #4
0
void main()
{
	AfxSocketInit();
	CSocket sock;
	int n = sock.Create(PORT1);
	if(!n)
	{
		cout << "创建句柄失败:" << GetLastError() << endl;
		return;
	}
	sock.Listen();

	while (true)
	{
		
		CSocket socka;
		n = sock.Accept(socka);
		if(!n)
		{
			cout << "接受对方连接失败:" << GetLastError() << endl;
			return;
		}
		while (true)
		{
			char s[256];
			n = socka.Receive(s,sizeof(s));
			if(SOCKET_ERROR == n)
			{
				cout << "接收数据失败:" << GetLastError() << endl;
				break;
			}
			if(!n)
			{
				cout << "结束接收数据,句柄关闭!" << endl;
				break;
			}
			cout << s <<endl;
		}
		socka.Close();

	}
}