Solved

IdTCPServer and IdTCPClient

Posted on 2004-04-16
2
1,236 Views
Last Modified: 2009-12-16
Hi guys,

There are multiple clients connected to my server, but the correct response does not always go to the correct Client, am I doing something wrong or am I suppose to use other components ?

See my code below :

Thanks !!

Pieter



CLIENT :

procedure TfrmMain.Button1Click(Sender: TObject);
var
  sResponse, sService : String;
  PDIni : TIniFile;
  iCounter : Integer;
  AStream : TStringStream;
begin
  {Read from INI File}
  PDIni := TIniFile.Create(ExtractFilePath(ParamStr(0) + 'conf\client.ini');

  iCounter := 1;

  try
    IdTCPClient1.Host := edtHost.Text;

    try
      IdTCPClient1.Port := StrToInt(edtPort.Text);
    except
      IdTCPClient1.Port := 7778;
    end;

    with IdTCPClient1 do
    begin

      Repeat
        sService := PDIni.ReadString('services', 'service_' + IntToStr(iCounter), '');

        Connect;

        while Connected do
        begin
          try
            AStream := TStringStream.Create('');

            WriteLn('service_status');
            WriteLn(sService);

            sResponse := ReadLn;

            mmLog.Lines.Add(sResponse);

            iCounter := iCounter + 1;

            AStream.Free;
          finally
            Disconnect;
          end;
        end;
      Until
        PDIni.ReadString('services', 'service_' + IntToStr(iCounter), '') = '';
    end;
  except
  end;
end;





SERVER :

procedure TMonitor.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  sRequest, sStatus : String;
begin
  try
    with AThread.Connection do
    begin
      try
        Repeat
          sRequest := ReadLn;

          if SRequest = 'service_status' then
          begin
            sServices := ReadLn;

            sStatus := GetServiceStat(PChar(sServices));

            if ((sStatus = 'UNKNOWN') or (sStatus = 'STOPPED')) then
            begin
              sStatus := 'N';
            end
            else if (sStatus = 'PAUSED') then
            begin
              sStatus := 'P';
            end
            else if ((sStatus = 'START/PENDING') or (sStatus = 'STOP/PENDING') or
              (sStatus = 'CONTINUE/PENDING') or (sStatus = 'PAUSE/PENDING')) then
            begin
              sStatus := 'B';
            end
            else
            begin
              sStatus := 'Y';
            end;

            WriteLn('[' + sServices + '][' + sStatus + ']');
          end;
        Until
          sRequest = '';
      finally
        Disconnect;
      end;
    end;
  except
  end;
end;

0
Comment
Question by:PieterJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 250 total points
ID: 10841299

   On the Server you should have TIdTCPServer component.

   Then add on the form TIdThreadMgrDefault VCL - found on Indy Misc tab. Select the TCP server and it has propery ThreadMgr - select the TIdThreadMgrDefault object there.
0
 

Author Comment

by:PieterJ
ID: 10841335
Hi Ivanov,

Thanks for the quick reply !!

Sorry, I forgot to mention that I do have a IdThreadMgrDefault Component in my Service, and have selected it in the IdTCPServer's Object Inspector.

Thanks

Pieter
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question