Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read string with indy using custom terminator

I am using a TIdTcpServer component to implement a basic server side application. I have clients witch send strings to the server,ascii encoded, ending in these to characters #@.

eg :

this_is_a_sample#@ thisis_another_sample#@

My OnExecute method is the following :

procedure TLX8511.ProcessEvent(AContext: TIdContext);
var
  recv : String;
begin
    with AContext.Connection.IOHandler do
    begin
          CheckForDataOnSource(20);
          if not InputBufferIsEmpty then
          begin
              recv := ReadLn('#@');
              WriteLn(recv);
          end;
    end;
end

However when the ReadLn is executed I receive a wierd error : Buffer terminator must be specified

What I am doing wrong ?

LE : I am using Indy with lazarus on linux so this might be some porting issue

Thank you.

like image 554
opc0de Avatar asked Jul 16 '26 22:07

opc0de


1 Answers

I don't like to answer my own questions but based of David Heffernan's suggestion I dug up the problem myself.

Turns out you must specify the encoding in order to avoid that exception so here is the solution:

procedure TLX8511.ProcessEvent(AContext: TIdContext);
var
  recv : String;
  encoding : IIdTextEncoding;
begin
    encoding := IndyTextEncoding_ASCII;
    with AContext.Connection.IOHandler do
    begin
          CheckForDataOnSource(20);
          if not InputBufferIsEmpty then
          begin
              recv := ReadLn('#@',encoding,encoding);
              WriteLn(recv);
          end;
    end;
end;  
like image 94
opc0de Avatar answered Jul 20 '26 12:07

opc0de