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 8111173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:00:41+00:00 2026-06-06T02:00:41+00:00

I am working with streaming packet strings through Indy sockets, and on the client

  • 0

I am working with streaming packet strings through Indy sockets, and on the client side, I have a thread which reads incoming data from the TIdTCPClient and continuously appending this data to the end of a single string buffer. I have another thread which is continuously reading this buffer from the beginning, copying (and deleting) data as necessary (one complete packet at a time).

I know that in any case two threads accessing the same variable can be dangerous. But does this apply to strings too? Or just objects? Can I feel safe with this reading/writing of the same string from two different threads? If not, then what should I do to protect this string? This is a plain string called FBuffer.

I’m appending data to the end like so:

procedure TListenThread.CheckForData;
begin
  if FClientSocket.Connected then begin
    FClientSocket.IOHandler.CheckForDataOnSource(5000);
    if not FClientSocket.IOHandler.InputBufferIsEmpty then
      FBuffer:= FBuffer + FClientSocket.IOHandler.InputBufferAsString;
  end;
end;

And the other thread is reading it like so:

procedeure TPacketThread.CheckForPacket;
var
  P: Integer; //Deliminator position
  T: String;  //Temp copying string
  Z: Integer; //Expected packet size
begin
  P:= Pos('#', FBuffer);
  if P > 0 then begin //Is the deliminator found?
    T:= Copy(FBuffer, 1, P-1); //Copy up to deliminator...
    Z:= StrToIntDef(T, 0); //Convert packet size to integer...
    if Z > 0 then begin
      //Is there a full packet waiting in buffer?
      if Length(FBuffer) >= Z then begin
        //First, delete size definition and deliminator...
        Delete(FBuffer, 1, P);
        //Now grab the rest of it up to the packet size...
        T:= Copy(FBuffer, 1, Z);
        //Delete what we just copied...
        Delete(FBuffer, 1, Z);
        //Finally, pass this packet string for further processing...
        ProcessPacket(T);
      end;
    end;
  end;
end;

The code is a simplified version of my code just to demonstrate all the work I need to do with FBuffer.

  • 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-06T02:00:43+00:00Added an answer on June 6, 2026 at 2:00 am

    Yes, you must protect the string buffer from concurrent access. Indy has a TIdThreadSafeString class you can use for that purpose, eg:

    FBuffer: TIdThreadSafeString;
    // make sure to Create() and Free() as needed..
    

    .

    procedure TListenThread.CheckForData; 
    begin 
      if FClientSocket.Connected then begin 
        FClientSocket.IOHandler.CheckForDataOnSource(5000); 
        if not FClientSocket.IOHandler.InputBufferIsEmpty then 
          FBuffer.Append(FClientSocket.IOHandler.InputBufferAsString);
      end; 
    end; 
    

    .

    procedure TPacketThread.CheckForPacket; 
    var 
      P: Integer; //Deliminator position 
      T: String;  //Temp copying string 
      Z: Integer; //Expected packet size 
    begin 
      FBuffer.Lock;
      try
        P:= Pos('#', FBuffer.Value); 
        if P > 0 then begin //Is the deliminator found? 
          T := Copy(FBuffer.Value, 1, P-1); //Copy up to deliminator... 
          Z := StrToIntDef(T, 0); //Convert packet size to integer... 
          if Z > 0 then begin 
            //Is there a full packet waiting in buffer? 
            if Length(FBuffer.Value) >= Z then begin 
              //First, delete size definition and deliminator... 
              FBuffer.Value := Copy(FBuffer.Value, P+1, MaxInt); 
              //Now grab the rest of it up to the packet size... 
              T := Copy(FBuffer.Value, 1, Z); 
              //Delete what we just copied... 
              FBuffer.Value := Copy(FBuffer.Value, Z+1, MaxInt); 
              //Finally, pass this packet string for further processing... 
              ProcessPacket(T); 
            end; 
          end; 
        end; 
      finally
        FBuffer.Unlock;
      end;
    end; 
    

    With that said, given what you have shown about the packet formatting, I would take a different tactic instead:

    FBuffer: TIdThreadSafeStringList;
    // make sure to Create() and Free() as needed..
    

    .

    procedure TListenThread.CheckForData; 
    var 
      T: String;  //Temp copying string 
      Z: Integer; //Expected packet size 
    begin 
      if FClientSocket.Connected then begin 
        if FClientSocket.IOHandler.InputBufferIsEmpty then begin
          FClientSocket.IOHandler.CheckForDataOnSource(5000);
          if FClientSocket.IOHandler.InputBufferIsEmpty then Exit;
        end; 
        // data is available, keep reading as long as packets are present...
        repeat
          T := FClientSocket.IOHandler.ReadLn('#');
          Z := StrToIntDef(T, 0);
          if Z > 0 then begin 
            T := FClientSocket.IOHandler.ReadString(Z); 
            FBuffer.Add(T); 
          end; 
        until FClientSocket.IOHandler.InputBufferIsEmpty;
      end; 
    end; 
    

    .

    procedure TPacketThread.CheckForPacket; 
    var 
      L: TStringList;
      T: String;
    begin 
      L := FBuffer.Lock;
      try
        if L.Count = 0 then Exit;
        T := L[0];
        L.Delete(0);
      finally
        FBuffer.Unlock;
      end;
      ProcessPacket(T); 
    end; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on android application in which i have play online radio streaming.
I am working on a structured data analysis framework which is based on streaming
I have smooth streaming working on my local machine. The video plays nicely and
I'm working on a program processing streaming data based on MS-RTSP protocol and it's
OK. AvPlayer is working great with streaming audio. In my app I have UISlider
While working on decoding some video streaming standards I have noticed a lot of
I'm working on an rtsp streaming(AAC format) client for iOS using ffmpeg. Right now
I am working on streaming a song from the web. I have made the
I'm working on a streaming project. I have VLC running as a server, streaming
Currently I am working on audio streaming on android. All method I have written

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.