0

How to use IdThreadComponent with TIdyTCPServer in c++ builder????

Please help!

Suhrob Samiev
  • 1,528
  • 1
  • 25
  • 57

2 Answers2

1

TIdTCPServer is multi-threaded internally for you. You don't need to use TIdThread or TIdThreadComponent directly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hi Remy first thanks for being F1 for me! That's I can use AContext for this purposes ? So how can I respond to specific client's request using AContext? OnConnect event how to add client's AContext to ListBox component and send data to specific client from the ListBox ? Also I looked over your post here https://forums.codegear.com/thread.jspa?threadID=41889&tstart=0 that also helped me! – Suhrob Samiev Apr 15 '11 at 03:13
  • Yes, TIdContext represents a specific client connection on the server. In the `OnConnect` and `OnDisconnect` events, use the `TIdSync` or `TIdNotify` class to update your UI safely. Then look for the desired TIdContext object in the server's `Contexts` list when needed. I have posted examples numerous times before, in the Embarcadero forums, the AToZed forms, and on StackOverflow. – Remy Lebeau Apr 19 '11 at 19:35
0

You can reach context on handling methods directly

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

}

Messages can be read onExecute event

AContext->Connection->Socket->ReadBytes(buf, 4, false);

Also in anywhere of your program, you can reach the context like that:

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();
Yunus Yurtturk
  • 509
  • 1
  • 8
  • 26