如何在c ++ builder中使用IdThreadComponent

问题描述

| 如何在C ++ Builder中将IdThreadComponent与TIdyTcpserver一起使用? 请帮忙!     

解决方法

TIdTCPServer
是内部多线程的。您不需要直接使用
TIdThread
TIdThreadComponent
。     ,您可以直接获取有关处理方法的上下文
void __fastcall TCPServer::OnDisconnect(TIdContext *AContext){
    AContext->Binding()->PeerIP //Returns IP Of the just connected client
        AContext->Binding()->PeerPort;

}
消息可以在onExecute事件中读取
AContext->Connection->Socket->ReadBytes(buf,4,false);
同样在程序的任何地方,您都可以像这样到达上下文:
TList *list = IdTCPServer1->Contexts->LockList();
         for(int i=0; i<IdTCPServer1->Contexts->LockList()->Count; i++){
            TIdContext *AContext = (TIdContext*)(list->Items[i]);
            if(AContext ->Binding()->PeerIP ==  clientIP){  // say you want to reach the context of a specified IP
                //Do something
            }
         }
        IdTCPServer1->Contexts->UnlockList();