Sending and receiving a stream using TidTCPClient and TidTCPServer in Delphi XE2

In Delphi XE2, I have a record type with the following structure:

TMachinInfoRec = record IPStr: string[15]; Username: string[50]; Computername: string[100]; SentTime: TDateTime; HasCommand: integer; ClientCommands: array[0..9] of TMachineCommand; end; 

I define a variable on my own and a TMemoryStream variable on the client side and send a stream with the TidTCPClient component:

 var MIRec: TMachinInfoRec; msRecInfo: TMemoryStream; begin MIRec.IPStr = '192.168.100.101'; MIRec.Username := 'user-a'; MIRec.Computername := 'Computer-a'; MIRec.SentTime := Now(); idTCPClient.Host := '192.168.100.138'; idTCPClient.Port := 6000; idTCPClient.Connect; msRecInfo := TMemoryStream.Create; msRecInfo.Write(msRecInfo, SizeOf(Client)); msRecInfo.Position := 0; idTCPClient.IOHandler.Write(msRecInfo); end; 

and get server-side information using TidTCPServer:

 procedure TFrmMainServer.TCPServerExecute(AContext: TIdContext); var MIRec: TMachinInfoRec; msRecInfo: TMemoryStream; begin msRecInfo:= TMemoryStream.Create; AContext.Connection.IOHandler.ReadStream(msRecInfo, SizeOf(MIRec)); msRecInfo.Read(msRecInfo, sizeOf(MIRec)); ShowMessage(MIRec.IPStr); ShowMessage(MIRec.Computername) end; 

But the line displayed in this format:

MZ? ......... yy .., ....... @ ........................... ........, ....

How can I solve this problem?

+6
source share
1 answer

Not worth it

  msRecInfo.Write(msRecInfo, SizeOf(Client)); 

will be

  msRecInfo.Write(miRec, SizeOf(miRec)); 

The same for reading:

  msRecInfo.Read(miRec, sizeOf(MIRec)); 

Please note that with this code there are several other undefined factors:

  • What is a customer? OTOH, with the above fixes, this is fixed.
  • We cannot confirm from this code that TMachineCommand is not a pointer type
+7
source

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


All Articles