Solved

Help me in chat application !

Posted on 2006-07-19
10
202 Views
Last Modified: 2010-04-05
I used indy TCP to make chat application both in client and server.

after a time (about 3 hours upper), CPU of server.exe near 100%, make my system LAG

I know function sleep() can solve this problem but I dont know where to put function sleep in.
 
in client code or in server code,

or is there a better way for server.exe than use sleep ?

please help me in detail.

thank all experts.

0
Comment
Question by:lhquang
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17137269
you will put a sleep(1) tipically or other small value in  loopes that take "forever" without doing anything. like

while not terminated do
  slee(100);

it is also advisable that you put application.processmessages; calls in such repetitive structures.
also, when doing client-server applications, you must use threads at least on teh server side to habdle client connections.
there are also other general issues that can cause a cpu eating up 100%.

so better post the code here so we can see exactly where those issues are. (maybe sleep is not the answer ;) )
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
ID: 17137656
I don't think Sleep() will solve this problem. All sleep does is halt the current thread and give another thread some CPU time. In general you would use Sleep(0) and not Sleep(1) if you just want another thread to start processing a bit more. (A Sleep(1) would add a delay of one millisecond, which might slow down your app considerably if you execute it often enough.)

The question is of course why your system needs so much CPU time. Maybe you're running out of memory because of a memory leak. Then it would be swapping to the swapfile. Or maybe something else is going wrong. Are you testing on multiple computers, where one is running the server and the other the client(s)? If so, which computer gets the 100% CPU?

It could also be that your application is calling Application.ProcessMessages or Sleep() too often. But I don't think Sleep() will solve it.

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17140667
the server runs its' clent connections inside a thread, so you do not need sleep
I would look for the problem elsewhere
can you post some of your code, and we might be able to spot the real problem
0
Independent Software Vendors: 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!

 
LVL 5

Expert Comment

by:Scay7
ID: 17140897
Have to agree here with TheRealLoki, we do not have enough information to help you. Also as Loki said its not a thread problem more likely a source twitch...
So paste you code where you think it use the most CPU time...

Peace Scay7
0
 

Author Comment

by:lhquang
ID: 17142850
the source code I developed from the chat demo in INDY 9. thank experts
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17143273
The Indy 9 chat demo is very buggy
firstly, the client can lock up on the
Msg := IdTCPClient1.ReadLn('', 5);
line in the Timer1Timer() event

This can be resolved by putting a TIdAntiFreeze on the form, but you still get a memory leak
(just watch the task maanger while your client is connected)
To resolve this you need to disable the TTimer while doing the ReadLn, so your Timer1Timer routine ends up looking something like this :-

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Com,
  Msg : String;
begin
  if not IdTcpClient1.Connected then
    exit;
  Timer1.Enabled := False;
  try
    Msg := IdTCPClient1.ReadLn('', 5);

    if Msg <> '' then
      if Msg[1] <> '@' then
        begin
        { Not a system command }
          memLines.Lines.Add(Msg);
        end
      else
        begin
        { System command }
          Com := UpperCase(Trim(Copy(Msg, 2, Pos(':', Msg) -2)));
          Msg := UpperCase(Trim(Copy(Msg, Pos(':', Msg) +1, Length(Msg))));
          if Com = 'CLIENTS' then
            lbClients.Items.CommaText := Msg;
        end;
    finally
      Timer1.Enabled := True;
    end;
  end;

anyway, this may be the cause of your problems...
0
 

Author Comment

by:lhquang
ID: 17143537
anyone can give me the better demo than INDY DEMO ?
thanks
0
 

Author Comment

by:lhquang
ID: 17151179
oh, now I found out the my problem and fixed it.

thank everybody
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 17152576
Am just curious... What was the error exactly? :-)
Which reminds me... memlines.lines.add just keeps adding strings to a memo field. Does it also clean up this memo a bit? If not, lots of memory usage there...
0
 

Author Comment

by:lhquang
ID: 17154681
Workshop_Alex , u r right, memlines.lines.add just keeps adding strings to a memo field and I solved it
0

Featured Post

Independent Software Vendors: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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