Example #1
0
int main(int argc, char** argv)
{
    if(!DbInit()){
	cout<<"DbInit() failed!"<<endl;
	exit(-1);
    }
    cout<<"BDB init finished!"<<endl;

    MSocket server;
    int port=PORT;
    if(server.Create()==false){
	cout<<"socket create error"<<endl;
	return 0;
    }
    //server.SetNonblock(false);
    if(server.Bind(port) != 0){
	cout<<"socket bind error!"<<endl;
	return 0;
    }
    if(server.Listen()!=0){
	cout<<"socket listen error"<<endl;
	return 0;
    }

    MThread threadPool[THREAD_MAX];
    for(int i=0;i<THREAD_MAX;i++){
	threadPool[i].Create(handleClientThread,(void*)&threadPool[i]);
    }
    MSocket* pclient=NULL;
    while(true){
	pclient=server.Accept();    //pclient need to be deleted
	if(pclient!=NULL){          //if blocking, NULL means accept ERROR
	    cout<<"Server Accept connect socket-------------------------------"<<endl;
	    MutexClient.Lock();
	    clientDeque.push_back(pclient);
	    MutexClient.Unlock();
	    
	}else
	{
	    cout<<"test: Blocking Socket Accept error! now sleep(100)"<<endl;
	    MThread::Sleep(100);
	}
    }
    server.Close();


    return 0;
}