--- In [EMAIL PROTECTED], "Rob Kennedy" <[EMAIL PROTECTED]> wrote:
>
> dts124 wrote:
> > Problem:
> > after 1-st client connection CPU usage of this service is 50%(probably
> > because my CPU is Pentium D), after 2nd - 99% and remains 99% until
> > service is stopped regardless that all clients are disconnected.
> >
> > After i added Sleep(10); into
> >   "while not Terminated do" loop of procedure TEchoThread.Execute;
> > CPU usage dropped to 0, now it seems OK, but is this the right way to
> > arrange service with threads for socket connections?
> 
> You should not need any loop at all. Why do you have it? Post the
code of
> your Execute method.
> 
> -- 
> Rob
>

Thank you for answering Rob!

procedure TEchoThread.Execute;
var
  Str: string;
begin
  while not Terminated do  // loop around until we should stop
  begin
    if FConnected then
    begin
      MessageBeep(MB_OK);
      FConnected := false;
    end;

    if FNewData then
    begin
      MessageBeep(MB_ICONEXCLAMATION);
      FNewData := false;
      Str := IntToStr(FSockHnd) +' '+ IntToStr(FConnIndx) +' '+ FBuff;
      Service1.ServerSocket1.Socket.Connections[FConnIndx].SendText(Str);
    end;

    Sleep(10);
  end;
end;

Here is fragment of the MainServiceUnit:

procedure TService1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i: integer;
begin
  NewThread.NewData := true;
  NewThread.Buff := Socket.ReceiveText;
  NewThread.SockHnd := Socket.SocketHandle;

  // which connection this data came from?
  for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
    if ServerSocket1.Socket.Connections[i].SocketHandle =
         Socket.SocketHandle then
      NewThread.ConnIndx := i;
end;

In real application after client establishes TCP connection it sends
ASSOCCIATION REQUEST message to which service answers with
ASSOCCIATION ACCEPT, then client sends C-FIND-RQ request to which
service answers with C-FIND-RESPONSE data and dialog ends with
RELEASE-REQUEST  / RELEASE-RESPONCE after which client disconnects. 
I think that processing one connection in one thread is good idea,
because message excange goes swiftly without any pause.
There could be more than 20 clients requesting data each minute.
The question is what is the best way to make Thread.Execute wait for
the next data block from the client it is serving with out waisting
too much CPU time.
Or maybe there is different approach?
Only alternative i see is to create new thread on every
ServerSocket1ClientRead, but that do not look attractive.

Dmitry

Reply via email to