Ok, the sluggishness is to be expected. But you stated it happens after like 5 times. It should already happen after the initial connection (thus the first time the client sends data).
Let look at the following:
repeat
S := ASocket.RecvString(120000);
memo1.lines.add('Client : '+S);
ASocket.SendString('Ok' + CRLF);
until ASocket.lasterror <> 0;
You need to know that any loop that you create has the potential to block the entire program. And sockets in Synapse are "blocking". (Why do you think they are called TBlockSockets)
Let me explain this another way. If you have a procedure and you do a loop in it, the program has no change to do anything else. You program is running in one "thread". So all commands you see are executed after each other. So a loop in your procedure will block the program from responding to anything else.
Try to but a button on your form with the following:
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
Button1.Caption := 'Busy';
for I := 1 to 10 do
begin
sleep(1000); // 1000ms is 1 second
end;
Button1.Caption := 'Ok';
end;
Now run the program and press that button. What do you see? Does the program still respons? Wait a bit... until the button-text goes "Ok". Does your program respond now? Yes.
So while running a loop in your program, your program stops all other execution (like responding to your mouse).
We can fix that with a Application.Processmessages;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
Button1.Caption := 'Busy';
for I := 1 to 10 do
begin
Application.Processmessages; // <-- add this
sleep(1000); // 1000ms is 1 second
end;
Button1.Caption := 'Ok';
end;
Now you can see that you can move the form while the loop is running... but the program is still a bit sluggish. That's because the sleep(1000) still holds the program captive for that one second each time. And we can't insert an processmessages in that sleep().
Now loop again at your procedure/loop:
repeat
S := ASocket.RecvString(120000); // this acts just like a sleep
memo1.lines.add('Client : '+S);
ASocket.SendString('Ok' + CRLF);
until ASocket.lasterror <> 0;
Because RecvString tries to receive a string it waits until that string arrives or times out after 120000ms (=120 seconds).
So essentially you get this (IF the client doesn't send a string):
repeat
sleep(120000); // 120 seconds
memo1.lines.add('Client : '+S);
ASocket.SendString('Ok' + CRLF);
until ASocket.lasterror <> 0;
You see, that's almost the same code as the blocking loop.
If the client does send a string your program responds for a moment but very shortly it freezes up again.
There are two possible solutions (actually three).
One, Reduce the timeout on RecvString() to something very short.
Like RecvString(10)
In the beginning that will work but if the string is large, it will timeout before the string is received completely.
Two (better solution), check with CanRead(10) if there is data on the line. Here you can use a small timeout because it's just a check if there is data. The actual reading of the string can have a bigger timeout. So:
repeat
if ASocket.CanRead(10) then
begin
S := ASocket.RecvString(120000);
memo1.lines.add('Client : '+S);
ASocket.SendString('Ok' + CRLF);
end;
Apllication.Processmessages; // let's not forget this one, otherwise it still hangs
until ASocket.lasterror <> 0;
That would already be a major improvement. But it still wait a very small amount.
Three (best solutions), use threads. But first see if you understand all the above (so first try solution two). Then you can think about threads
Do you already know what threads are???