Solved

IdTCPServer and IdTCPClient

Posted on 2004-04-16
2
1,213 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
2 Comments
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 250 total points
Comment Utility

   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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now