Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send and Receive Stream with 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 its and TMemoryStream variable on the Client side and send stream with 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 And get information on the server side with 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 a string that is displayed in this format:

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

how can I solve this problem?

like image 399
Mohamad Avatar asked Feb 25 '12 10:02

Mohamad


1 Answers

Shouldn't

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

be

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

Same for read:

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

Note that there are several other uncertain factors with this code:

  • what is "client"? OTOH, with the above corrections, this is eliminated.
  • We can't confirm from this code that TMachineCommand is not a pointer type
like image 61
Marco van de Voort Avatar answered Oct 14 '22 03:10

Marco van de Voort