Solved

IdTCPServer and IdTCPClient

Posted on 2004-04-16
2
1,225 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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