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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:12:04+00:00 2026-06-09T16:12:04+00:00

I use this code to send a file to a client. procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

  • 0

I use this code to send a file to a client.

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
 hFile    : THandle;
 FileBuff : array [0..1023] of byte;
 dwRead   : DWORD;
 Recieved : String;
begin
 Recieved := Athread.Connection.ReadLn;
 if Recieved = 'FILE' then begin
   Memo1.Lines.Add('Sending File...');
   hFile := CreateFileW('FILE.bin',
   GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
   if hFile = INVALID_HANDLE_VALUE then exit;
   SetFilePointer(hFile, 0, nil, FILE_BEGIN);
   while true do begin
     ReadFile(hFile, FileBuff[0], 1024, dwRead, nil);
     if dwRead <= 0 then break;
     Athread.Connection.WriteBuffer(FileBuff[0], dwRead);
   end;
   CloseHandle (hFile);
   Athread.Connection.Disconnect;
 end;
end;

This works like a charm but if the client disconnects while the file is sending, Indy terminates the Execute Thread immediately so the filehandle is still open! Is there a way to close the filehandle after the client disconnects?

Thank you for your help.

  • 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-09T16:12:05+00:00Added an answer on June 9, 2026 at 4:12 pm

    Your code has three problems in it:

    1) accessing the TMemo directly is not thread-safe, so it can cause deadlocks and/or crashed. UI access must be done in the context of the main thread only. You can use Indy’s TIdSync or TIdNotify class to safely access the UI from server events.

    2) like RRUZ mentioned, you are not protecting the file handle from exceptions. If an exception is raised, like when a client disconnects, you are not closing the file handle.

    3) you are opening the file using a relative path. Always use absolute paths to ensure the correct file is used.

    Try this:

    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread); 
    var 
     hFile    : THandle; 
     FileBuff : array [0..1023] of byte; 
     dwRead   : DWORD; 
     Recieved : String; 
    begin 
      Recieved := Athread.Connection.ReadLn; 
      if Recieved = 'FILE' then begin 
        // I'll leave this as an exercise for you to figure out...
        //Memo1.Lines.Add('Sending File...'); 
    
        hFile := CreateFile('C:\Path\FILE.bin', GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0); 
        if hFile = INVALID_HANDLE_VALUE then RaiseLastOSError;
        try
          repeat 
            if not ReadFile(hFile, FileBuff[0], SizeOf(FileBuff), dwRead, nil) then RaiseLastOSError;
            if dwRead = 0 then Break; 
            AThread.Connection.WriteBuffer(FileBuff[0], dwRead);
          until False;
        finally
          CloseHandle(hFile); 
        end;
        AThread.Connection.Disconnect; 
      end; 
    end; 
    

    Alternatively, you can pass the filename to Indy and let it manage the file for you:

    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread); 
    var 
     Recieved : String; 
    begin 
      Recieved := Athread.Connection.ReadLn; 
      if Recieved = 'FILE' then begin 
        // I'll leave this as an exercise for you to figure out...
        //Memo1.Lines.Add('Sending File...'); 
    
        AThread.Connection.WriteFile('C:\Path\FILE.bin', True); 
        AThread.Connection.Disconnect; 
      end; 
    end; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When client request for a file, I use this code to send it: public
I use code below to send file from ASP.NET Rest Api to client. Sometimes
I want to send an SMS via intent, but when I use this code,
I use this code to send mail using java. In here i attach a
I use this code : MethodInvoker TileLoadCompleteMethodInvoker = delegate() { CleanStatusLabelTimer.Enabled = true; MemoryTextBox.Text
i use this code in my app. just found is not correct when compare
I use this code to connect to SQL Server 2012, but it does not
I use this code to select password field from the sql server 2012 db,
I use this code , to log $_SERVER['REMOTE_ADDR']; to my small db my issue
I use this code to update data in database table. Can reuse same code

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.