Delphi TidTCPServer and TidTCPClient transferring a record

Samir Memmedov picture Samir Memmedov · Jun 4, 2012 · Viewed 17k times · Source

I need help in understanding how to transfer a record through Indy TCP Server/Client. I have 2 programs, in I put client and in another server. On client on a button I put connect : Client is TIdTCPClient

Client.Connect();

And at server side I am adding a line to memo that client is connected , on ServerConnect event

Protocol.Lines.Add(TimeToStr(Time)+' connected ');

To send data from client I have a record, which I want to send :

Tmyrecord = record
IPStr: string[15];
end;

And I have a send button there :

procedure Tform1.ButtonSendClick(Sender: TObject);
  var
  MIRec: Tmyrecord;
 msRecInfo: TMemoryStream;
 begin
   MIRec.IPStr := '172.0.0.1';
   msRecInfo := TMemoryStream.Create;
   msRecInfo.Write(MIRec, SizeOf(MIRec));
    msRecInfo.Position := 0;
   Client.IOHandler.Write(msRecInfo);
 end;

At server side onexecute I have the following code , I have same tmyrecord declared at server side too :

 procedure TServerFrmMain.ServerExecute(AContext: TIdContext);
 var
  MIRec: Tmyrecord;
  msRecInfo: TMemoryStream;
 begin
 if  AContext.Connection.Connected then
  begin
    AContext.Connection.IOHandler.CheckForDataOnSource(10);
    if not AContext.Connection.IOHandler.InputBufferIsEmpty then
   begin
     msRecInfo:= TMemoryStream.Create;
       AContext.Connection.IOHandler.ReadStream(msRecInfo);
     msRecInfo.Read(MIRec, sizeOf(msRecInfo));
    ShowMessage(MIRec.IPStr);
 end;
 end;
 end

I dont know why it is not working, why I cant show IP adress which I wrote from client side. I want to read a record (msRecInfo) on server side which I am sending from client side. I want to access my record elements, in this case I want to read IPSTR element of my record. When I press send button from a client side, application hangs, server part.

Thanks a lot in advance

Answer

Remy Lebeau picture Remy Lebeau · Jun 5, 2012

You are making a classic newbie mistake - you are expecting the default behaviors of the TIdIOHandler.Write(TStream) and TIdIOHandler.ReadStream() methods to match each other, but they actually do not.

The default parameter values of TIdIOHandler.ReadStream() tell it to expect an Integer or Int64 (depending on the value of the TIdIOHandler.LargeStream property) to preceed the stream data to specify the length of the data.

However, the default parameter values of TIdIOHandler.Write(TStream) do not tell it to send any such Integer/Int64 value. Thus, your use of TIdIOHandler.ReadStream() reads the first few bytes of the record and interprets them as an Integer/Int64 (which is 926036233 given the string value you are sending), and then waits for that many bytes to arrive, which never will so TIdIOHandler.ReadStream() does not exit (unless you set the TIdIOHandler.ReadTimeout property to a non-infinite value).

There are also some other minor bugs/typos in your code that uses the TMemoryStream objects outside of Indy.

Try this instead:

procedure Tform1.ButtonSendClick(Sender: TObject); 
var 
  MIRec: Tmyrecord; 
  msRecInfo: TMemoryStream; 
begin 
  MIRec.IPStr := '172.0.0.1'; 
  msRecInfo := TMemoryStream.Create; 
  try
    msRecInfo.Write(MIRec, SizeOf(MIRec)); 

    // writes the stream size then writes the stream data
    Client.IOHandler.Write(msRecInfo, 0, True);
  finally
    msRecInfo.Free;
  end;
end; 

procedure TServerFrmMain.ServerExecute(AContext: TIdContext); 
var 
  MIRec: Tmyrecord; 
  msRecInfo: TMemoryStream; 
begin 
  msRecInfo := TMemoryStream.Create; 
  try
    // reads the stream size then reads the stream data
    AContext.Connection.IOHandler.ReadStream(msRecInfo, -1, False);

    msRecInfo.Position := 0;
    msRecInfo.Read(MIRec, SizeOf(MIRec)); 
    ...
  finally
    msRecInfo.Free;
  end;
end;

Or this:

procedure Tform1.ButtonSendClick(Sender: TObject); 
var 
  MIRec: Tmyrecord; 
  msRecInfo: TMemoryStream; 
begin 
  MIRec.IPStr := '172.0.0.1'; 
  msRecInfo := TMemoryStream.Create; 
  try
    msRecInfo.Write(MIRec, SizeOf(MIRec)); 

    // does not write the stream size, just the stream data
    Client.IOHandler.Write(msRecInfo, 0, False); 
  finally
    msRecInfo.Free;
  end;
end; 

procedure TServerFrmMain.ServerExecute(AContext: TIdContext); 
var 
  MIRec: Tmyrecord; 
  msRecInfo: TMemoryStream; 
begin 
  msRecInfo := TMemoryStream.Create; 
  try
    // does not read the stream size, just the stream data
    AContext.Connection.IOHandler.ReadStream(msRecInfo, SizeOf(MIRec), False); 

    msRecInfo.Position := 0;
    msRecInfo.Read(MIRec, SizeOf(MIRec)); 
    ...
  finally
    msRecInfo.Free;
  end; 
end; 

Alternatively, you can send the record using TIdBytes instead of TStream:

procedure Tform1.ButtonSendClick(Sender: TObject); 
var 
  MIRec: Tmyrecord; 
  Buffer: TIdBytes;
begin 
  MIRec.IPStr := '172.0.0.1'; 
  Buffer := RawToBytes(MIRec, SizeOf(MIRec));
  Client.IOHandler.Write(Buffer); 
end; 

procedure TServerFrmMain.ServerExecute(AContext: TIdContext); 
var 
  MIRec: Tmyrecord; 
  Buffer: TIdBytes; 
begin 
  AContext.Connection.IOHandler.ReadBytes(Buffer, SizeOf(MIRec)); 
  BytesToRaw(Buffer, MIRec, SizeOf(MIRec));
  ...
end;