Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8046999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:58:03+00:00 2026-06-05T05:58:03+00:00

I need help in understanding how to transfer a record through Indy TCP Server/Client.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:58:04+00:00Added an answer on June 5, 2026 at 5:58 am

    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; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Kind of need help understanding what this code actually outputs. Does it out put
I have been trying out Cassandra and need some help in understanding a few
I have some what complex UI requirement, need help in understanding type of control/style/template
I need some help understanding how jQuery stores elements. Please have a look at
There are a few things I need help understanding. Long story short: Client has
I need help understanding the following. Say I have a display that is 854x480
I need help understanding the proper sequence of filtering data when sending user inputted
I need help understanding how to demonize a process in Go. package main import
Need some help understanding the difference between the success and ajaxSuccess events documented here
I need some help understanding the advantages of using CountDownLatch over traditional wait-notify. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.