Memo1.Lines.LoadFromStream from IOHandler.ReadStream

I tried to display the sent text file in memo.lines without saving it to disk

from server

try Ms := TMemoryStream.Create; Ms.LoadFromFile('update.txt'); Ms.Position := 0; AContext.Connection.IOHandler.LargeStream := True; AContext.Connection.IOHandler.Write(Ms, 0, True); finally Ms.Free; end; 

to the client ... im not sure how to do this in the client

  try Ms := TMemoryStream.Create; Ms.Position := 0; IdTCPClient1.IOHandler.LargeStream := True; IdTCPClient1.Connection.IOHandler.ReadStream(Ms, -1,false); finally Memo1.Lines.LoadFromStream(Ms); Ms.Free; end; 

Can someone help me on how to make this work, if possible?

+4
source share
2 answers

Your code is fine, you just forgot to reset the stream Position property back to 0 before calling the Memo LoadFromStream() method:

 IdTCPClient1.Connection.IOHandler.ReadStream(Ms, -1,false); Ms.Position := 0; // <-- add this Memo1.Lines.LoadFromStream(Ms); 
+3
source

If I were you, I would still buffer the read data for two reasons:

  • The network stream is unidirectional and non-positionable , so it is not a file. (Yes, this also means that you must update it manually / programmatically every time new data is received.)
  • If the connection fails, the reading buffer may be filled with a talisman that receives diplayed, because you do not have any additional means to control what is displayed as soon as you redirect the input of your note.

If you do not want to save the sent data to disk, you can still save it in an instance of TMemoryStream, which can be used as a parameter of memo.LoadFromStream () method.

0
source

Source: https://habr.com/ru/post/1495319/


All Articles