I have an error that is confusing the hell out of me. The following code sends a line from a TidTCPClient to a TidTCPServer. The first time it executes, it works perfectly. The second time it executes, and every time after that, it adds a line break to the start of every string. What am I missing? (I know it does it in an odd way, but the list of clients is necessary in the full code)
procedure TClientForm.ButtonSendStringClick(Sender: TObject);
var
I: integer;
List: TList;
begin
List := ClientList.LockList;
try
for I := 0 to (List.Count- 1) do
begin
TidTCPClient(List[I]).IOHandler.WriteLn('Hello'+'|x|');
end;
finally
ClientList.UnlockList;
end;
Edit1.Text := '';
end;
procedure TClientForm.IdTCPServer1Execute(AContext: TIdContext);
var
LLine: string;
begin
LLine := Acontext.Connection.IOHandler.ReadLn('|x|');
OutputDebugString(PChar(LLine));
end;
WriteLn()appends a CRLF to the end of the string you pass to it, butReadLn()stops reading when it encounters the terminator string that you specify. So you are sending'Hello|x|#13#10'but you are only reading'Hello|x|', leaving#13#10in the socket buffer for the next read to grab.To solve your problem, you have two choices:
1) If you want to keep using a custom terminator in
ReadLn(), changeWriteLn()toWrite()so the implicit CRLF is not sent anymore. No change is needed in yourReadLn()call.2) Stop using a custom terminator altogether. Pass just your main string by itself to
WriteLn()and let it append a CRLF, then do not pass any terminator toReadLn()as its default terminator is LF (which includes handling for CRLF).