Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending stringlist as chunks
#1
i have got tstringlist with 1000 lines that i want to send its text from idtcpserver to a clientside

i did not want to send this stringlist as memorystream i want to avoid using memorystream at all i will use writeln to write this stringlist.text

but i am trying to find a way to separate this string list and send it content separately

as example sending each 100 lines until i reach the end of lines

but couldn't figure out a way in coding to do this purpose
Reply
#2
Look at TCP Client write methods, I'm quite sure an overload exists already for TStrings.

There is no need to chunk it, and doing so could cause packet fragmentation.
Reply
#3
(03-18-2018, 03:52 PM)kudzu Wrote: I'm quite sure an overload exists already for TStrings.

Indeed, there is. TIdIOHandler has two methods for sending a TStrings:
  • Write(TStrings)

    Code:
    procedure Write(AValue: TStrings; AWriteLinesCount: Boolean = False;  AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}); overload; virtual;

    If AWriteLinesCount=True, the TStrings.Count value will be sent first as a 32-bit integer in network byte order using TIdIOHandler.Write(Int32). Then the strings will be sent individually using TIdIOHandler.WriteLn().

    If the receiver is also using Indy, the strings can be read using TIdIOHandler.ReadStrings():

    Code:
    procedure ReadStrings(ADest: TStrings; AReadLinesCount: Integer = -1;
      AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF} );

    If AReadLinesCount=-1, the count will be read from the socket as a 32-bit integer in network byte order using TIdIOHandler.ReadInt32(). The specified number of strings will be read using TIdIOHandler.ReadLn().

  • WriteRFCStrings()

    Code:
    procedure WriteRFCStrings(AStrings: TStrings; AWriteTerminator: Boolean = True;
      AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF});

    The strings will be sent individually using TIdIOHandler.WriteLnRFC() (which escapes a leading '.' character as '..'). Then, if AWriteTerminator=True, '.' will be sent using TIdIOHandler.WriteLn().

    If the receiver is also using Indy, the strings can be read using one of the TStrings overloaded versions of TIdIOHandler.Capture():

    Code:
    procedure Capture(ADest: TStrings; AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}); overload; // .Net overload

    procedure Capture(ADest: TStrings; const ADelim: string;
      AUsesDotTransparency: Boolean = True; AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}); overload;

    procedure Capture(ADest: TStrings; out VLineCount: Integer;
      const ADelim: string = '.'; AUsesDotTransparency: Boolean = True;
      AByteEncoding: IIdTextEncoding = nil
      {$IFDEF STRING_IS_ANSI}; ADestEncoding: IIdTextEncoding = nil{$ENDIF}); overload;

    The strings will be read from the socket using TIdIOHandler.ReadLn(), unescaping leading '..' to '.' if AUsesDotTransparency=True, until the specified ADelim string ('.' by default) is reached.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)