I have c:\ test.txt = 300мб . Transfer TcpClient and TcpServer . Do not send large files!

Existing 100% working code for small files:

 procedure TForm1.Button3Click(Sender: TObject); var fileName: WideString; fileSize: Int64; dataFile: PByte; hFile: File; begin if TcpClient1.Connect then begin TcpClient1.Sendln('dlmain'); fileName := TcpClient1.Receiveln(); fileSize := StrToInt64(TcpClient1.Receiveln()); dataFile := AllocMem(fileSize); try TcpClient1.ReceiveBuf(dataFile^, fileSize); if SaveDialog1.Execute(Handle) then begin AssignFile(hFile, SaveDialog1.fileName); Rewrite(hFile, 1); BlockWrite(hFile, dataFile^, fileSize); CloseFile(hFile); end; finally FreeMem(dataFile); end; TcpClient1.Disconnect; end; end; procedure TForm1.FormCreate(Sender: TObject); begin TcpServer1.Active := True; end; procedure TForm1.FormDestroy(Sender: TObject); begin TcpServer1.Active := False; end; procedure TForm1.IdTCPServer1Execute(AContext: TIdContext); var S: string; f: file of byte; fs, fn: string; mem: TmemoryStream; fStream: TFileStream; begin end; procedure TForm1.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient); var msg: WideString; hFile: TFileStream; fileName: WideString; begin fileName := 'C:\ test.txt '; msg := ClientSocket.Receiveln(); if msg = 'dlmain' then begin hFile := TFileStream.Create(fileName, fmOpenRead); try ClientSocket.Sendln(fileName); ClientSocket.Sendln(IntToStr(hFile.Size)); ClientSocket.SendStream(hFile); finally hFile.Free; end; end; end; end. 
  • Do not forget to format the code with the button 101010 . - Nofate
  • ? explain! What is the 101010? - kindos
  • 3
    0_O is it in the delph or here? - kindos
  • Please provide the source client. - user213238
  • 2
    @SWNTFZ question for four years, the author is unlikely to be able to add something. - Nick Volynkin

2 answers 2

In fact, everything is sent: both small files and large ones. It’s just that it takes more time to transfer "large" files, and network traffic too ... In this connection, it seems that the application is frozen, right? In fact, this is only an "illusion" - the main thread has switched all its "power" =) to another task. You should use multithreading in your application. The main thread will be occupied, directly, by the application itself, and the auxiliary one, which you will create, by sending the file. In the dolphi is very simple

    @kindoskin , send the file in chunks. For example, the size of 1024 bytes.

    • I will be grateful if there is a demo.zip :))) or at least a code. I'm using xe2. - kindos