【问题标题】:How to use IdThreadComponent in c++ builder如何在 c++ builder 中使用 IdThreadComponent
【发布时间】:2011-04-14 08:04:55
【问题描述】:

如何在 c++ builder 中使用 IdThreadComponent 和 TIdyTCPServer????

请帮忙!

【问题讨论】:

    标签: multithreading c++builder indy


    【解决方案1】:

    TIdTCPServer 在内部为您提供多线程。您无需直接使用TIdThreadTIdThreadComponent

    【讨论】:

    • 嗨,Remy 首先感谢您成为我的 F1!那就是我可以为此目的使用 AContext 吗?那么如何使用 AContext 响应特定客户的请求呢? OnConnect 事件如何将客户端的 AContext 添加到 ListBox 组件并从 ListBox 向特定客户端发送数据?我还在这里查看了您的帖子forums.codegear.com/thread.jspa?threadID=41889&tstart=0,这对我也有帮助!
    • 是的,TIdContext 代表服务器上的特定客户端连接。在OnConnectOnDisconnect 事件中,使用TIdSyncTIdNotify 类来安全地更新您的UI。然后在需要时在服务器的Contexts 列表中查找所需的 TIdContext 对象。我之前在 Embarcadero 论坛、AToZed 表单和 StackOverflow 上发布过无数次示例。
    【解决方案2】:

    您可以直接访问处理方法的上下文

    void __fastcall TCPServer::OnDisconnect(TIdContext *AContext){
        AContext->Binding()->PeerIP //Returns IP Of the just connected client
            AContext->Binding()->PeerPort;
    
    }
    

    消息可以在执行事件中读取

    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();
    

    【讨论】:

      猜你喜欢
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多