How to send record over TCP (LNet/TStringList/Stream) lazarus / 13-General ---------------------------------------------------------------------- katyan · 2007-03-14 07:56 Hi, I have want to write small chat application. I'm using LNet to send/recieve data over NET. I would like to send this record: TPakiet=packed record     flags:string[10];     src:integer;     dest:integer;     nr:integer;     data:string[255]; - works And this is working, but I'm limited to send only 255 chars. I want tu use TMemo component and save all lines to TStringList and this isn't working. Or maybe convert it to stream. I tried  to create TMemoryStream, but when I wanted to read from it I get empty record. Any ideas to solve this? Thanks for all answers. ---------------------------------------------------------------------- theo · 2007-03-14 11:24 The Problem is, that you're only sending a Pointer in case of TStringlist. You could use my DataPacker http://www.theo.ch/lazarus/uDataPacker.pas Pack all your Data separately in the Packer like dp:=TMemoryDataPacker.create; dp.AddString(flags);     dp.AddInt(src) ; etc. Then send dp.Stream. dp.free; On the other side receive dp.Stream then do: flags:=dp.GetString; src:=dp.GetInt; etc. This must be in the same order as you packed it. You can also give a name to the items like: dp.AddString('flags',flags); ... dp.GetString('flags'); In this case, they don't have to be in order. You can also Compress the data before sending: dp.Compress; Then the first thing you do on the receive-side after reading in dp.Stream: dp.DeCompress; P.S. I forgot you can add the "fixed size" part of your record also with dp.AddBuffer(MyRec,SizeOf(TMyRec)); .... dp.GetBuffer(MyRec); But you have to add the LongString separately anyway. ---------------------------------------------------------------------- katyan · 2007-03-14 14:07 Thanks for your post theo. Your DataPacker works fine, but it didn't solve my problem. In LNet compnent  I only have option to send data or text - I can't send stream. So is there another way to do this? ---------------------------------------------------------------------- theo · 2007-03-14 14:37 Since it's a TMemoryStream the "Data" is in dp.Stream.Memory^; ;-) ---------------------------------------------------------------------- katyan · 2007-03-15 13:00 OK I can send dp.stream.Memory^. Send command in LNet is Send(data,size) so in first place I'm sending size of dp (dp.stream.getsize). What I do next: Server side: FNet.Socks[1].Send(dp.Stream.Memory^ ,dp.Stream.GetSize); Client side: if (aSocket.Get(dp.stream.memory^,ile))>0 then ... ile - contains size of dp. .. and it dissconect client from server. ---------------------------------------------------------------------- theo · 2007-03-15 14:02 I've never used INet, always Synapse http://synapse.ararat.cz/ Synapse has a funtion RecvStream(const Stream: TStream; Timeout: Integer) So I don't know what "ile" is exactly. I assume it's the buffer size, i.e. the memory you should have allocated to the Stream before receiving, with: dp.Stream.Size:=ile; There are several ways to do this. It has little to do with my work. You should learn about working with TMemoryStream. If you know how much data is waiting, just set dp.Stream.size to this size. If you know the maximum data you expect. set dp.Stream.size to this MaxSize and after receiving to the result value of aSocket.Get. If you don't know anything, use a fixed size buffer (e.g. of size 1024) and repeat aSocket.Get until no more data is waiting. In this cycle, always write the Buffer to the Stream like: dp.Stream.Write(Buffer,ResultOfaSocketGet); But as I said, I don't know how INet works, I'm just guessing... ;-) If it's too complicated for you, just sent the text and record separately with Inet and forget about DataPacker. Could be slower though. ---------------------------------------------------------------------- katyan · 2007-03-20 16:39 Hi, Theo I tried to send dp.stream using synapse and I did something wrong. Could you write step by step how to send dp.stream and how to recieve it? Maybe you have an example (program) to show me the way... ---------------------------------------------------------------------- theo · 2007-03-20 18:41 I gave you an example how to use Datapacker above, The rest is only about sending and receiving a stream with the TCP/IP Library you choose. If you have problems with Synapse then have a look at the Synapse demos (echo example), or use INet if it's easier for you. I can't give you an introduction in TCP/IP programming here. ;-) ---------------------------------------------------------------------- katyan · 2007-03-20 19:30 OK thanks for everything