Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Synchronizing TCPServer with main thread
I'm building a server app that collects data from many clients (CPU loads) using Indy components (TIdTCPServer and TIdTCPClient). The server should periodically issue a REPORT command to each client and receive a LOAD X response.
I've set up the client app to create the TCPClient in a TThread descendant and after connecting to the server it just waits to receive a REPORT command, at which point the client answers with 'LOAD X', where X is an integer.
s := TCPClientInsideThread.IOHandler.ReadLn;
if ( s = 'REPORT' ) then TCPClientInsideThread.IOHandler.WriteLn('LOAD ' + IntToStr(GetLoads.CPULoad));
until s = 'DISCONNECT';
On the server side, I don't know how to synchronize the server-client connection with the main thread and I don't know where to issue the REPORT command.
I tried putting it in the OnExecute handler (using a relatively small timeout value to achieve repetition), but when more than one clients were connected, I started getting access violations.
I tried putting it in the OnTimer handler of a TTimer, where I cycled through each connection and sent a REPORT request, but that made the server hang after a few requests to even one client and then got access violations
When and where should I issue the REPORT command on the server? How should I collect the response from the client? How do I put the response in the main thread of the server?
Could someone please help out with ideas, if not code?