Beispiel #1
0
bool CTcp::Start(int port , tcpHandler handler)
{
	bool ret = FALSE;

	MySocket socket;

	socket.Create();

	if (socket.Bind(port))
	{
		m_sock = socket;

		ARGV_LIST* argv = new ARGV_LIST;

		argv->handler = handler;
		argv->s = socket;
		argv->lpParameter = this;

		listen(socket,25);
		_beginthread(Listen,0,argv);

		ret = TRUE;
	}

	return ret;


}
Beispiel #2
0
int main()
{
	/* 初始化协议 */ 
    StartSocket();
    GameProtocal gameProtocal;
	clientMgr.AttachProtocal(&gameProtocal);
    gameProtocal.AttachManager(&clientMgr);
    /* 开启监听 */ 
    sockSrv.Bind(MYPORT);
    sockSrv.Listen(); 
    clientMgr.AddServer(&sockSrv);
    cout << "Now Listenning..." << endl;

    // 开启服务器发包线程,传送数据到
    ThreadParam para;
    PThreadParam pp = &para;
    pp->mgr = &clientMgr;

#ifdef WIN32    
    DWORD  threadID;
    CreateThread(0, 0, LPTHREAD_START_ROUTINE(ThreadSend), pp,NULL, &threadID);
#else 
    pthread_t threadID;
    pthread_create(&threadID, NULL, ThreadSend, (void*)pp);
    //signal(SIGALRM   ,HeartBeatFun );       /*   alarm   clock   timeout   */  
#endif
	
	// 主循环
	while ( true )
    {  
        int len = sizeof(sockaddr);
        int sockConn; 
        /*  测试accept返回的socket  */
        if ( -1 == (sockConn = sockSrv.Accept(&SockConn)) ){
            exit(1);
        }

        /* 连接成功,创建新的客户socket以及线程,传送数据到thread */
    	ThreadParam para;
		PThreadParam pp = &para; 
		para.sock = &SockConn;
        para.mgr  = &clientMgr;

        /* 开启客户线程 */
#ifdef WIN32
		CloseHandle(CreateThread(0, 0, LPTHREAD_START_ROUTINE(ThreadClient),
            pp,NULL, &threadID) );
#else
        pthread_t threadID;
        pthread_create(&threadID, NULL, ThreadClient, (void*)pp);
#endif
	}

    /* 清场 */
	sockSrv.Close();
	DestroySocket();
    return 0;
}
Beispiel #3
0
int main(){

	MySocket s;
	SocketAddress a("localhost", 8080);
	s.Bind(&a);
	printf("Binded succesfully\n");
	Run(&s);
	return 0;
}