idtcp for file downloads and uploads

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  StdCtrls;

type
  TForm1 = class(TForm)
    btnReceive: TButton;
    IdTCPClient1: TIdTCPClient;
    btnSend: TButton;
    procedure btnReceiveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnReceiveClick(Sender: TObject);
var
    rbyte:array[0..4096] of byte;
    sFile:TFileStream;
    iFileSize:integer;
begin
  iFileSize:=IdTCPClient1.ReadInteger;

  sFile:=TFileStream.Create('xx.pdf',fmCreate);
  While iFileSize>4096 do
  begin
      IdTCPClient1.ReadBuffer(rbyte,4096);// .ReadBuffer(rbyte,iLen);
      sFile.Write(rByte,4096);
      inc(iFileSize,-4096);
  end;
  IdTCPClient1.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen);
  sFile.Write(rByte,iFileSize);
  sFile.Free;
  ShowMessage('file get ok!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//  IdTCPClient1.BoundIP:= '127.0.0.1';
//  IdTCPClient1.BoundPort:= 90011;

  IdTCPClient1.Host:= '127.0.0.1';
  IdTCPClient1.Port:= 9000;


  IdTcpClient1.Connect(5000);
  self.Caption:= '启动';
end;

procedure TForm1.btnSendClick(Sender: TObject);
var
    iFileHandle:integer;
    iFileLen,cnt:integer;
    buf:array[0..4096] of byte;

begin
//  IdTCPClient1.Host:='127.0.0.1';
//  IdTCPClient1.Port:=9000;


    //IdTCPClient1.Connect(5000);


  if IdTCPClient1.Connected then
  begin
    iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
    iFileLen:=FileSeek(iFileHandle,0,2); //2 :soFromBeginning : from the end of the file
    FileSeek(iFileHandle,0,0);
//    ProgressBar1.Max:=iFileLen;
//    ProgressBar1.Position := 0;
    IdTCPClient1.WriteLn('xx.pdf'+'|'+IntToStr(iFileLen));
    the while to true do 
    the begin 
      Application.ProcessMessages by; 
      CNT: = FileRead (iFileHandle, buf, 4096 ); 
      IdTCPClient1.WriteBuffer (buf, CNT); 
     // IdTCPClient1.WriteInteger (CNT); 
     
//       ProgressBar1.Position: = CNT + ProgressBar1.Position ; 
//       StatusBar1.Panels [0] .Text: = 'Sending file ...'; 
      IF CNT < 4096  the then 
          BREAK ;
     End ; 

    the FileClose (iFileHandle); 
//     Label2.Caption: = 'file transfer is complete!'; 
//     StatusBar1.Panels [0] .Text: = 'file transfer is complete!'; 
   End ;
 End ;

end.






unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;

type
  TForm1 = class(TForm)
    IdTCPServer1: TIdTCPServer;
    procedure IdTCPServer1Execute(AThread: TIdPeerThread);
    procedure FormCreate(Sender: TObject);
    procedure IdTCPServer1Connect(AThread: TIdPeerThread);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1; 

Implementation 

{ $ R & lt *. Dfm } 
uses IdSocketHandle; 


// Server receives 
Procedure TForm1.IdTCPServer1Execute (AThread: TIdPeerThread);
 var 
  rbyte: Array [ 0 .. 4096 ] of byte; 
  sFile: TFileStream; 
  CNT, cmd, FileSize: Integer; 
  STR, fileName: String ;
 the begin 
    STR: = AThread.Connection.ReadLn;    // receiving a file size and file name 

    cmd: = POS ( ' | ' , STR); // Find delimiter
    FileName: = Copy (str, 1 , cmd- 1 ); // extract the file name 
    // IdTCPClient1.WriteLn (ExtractFileName (edtFileName.Text) + '|' + IntToStr (iFileLen)); 
    FileSize: = StrToInt (Copy (str , cmd + 1 , the Length (str) -cmd + 1 )); // extract the file size 
    IF MessageBox ( 0 , Pchar ( ' user ' + AThread.Connection.Socket.Binding.PeerIP + ' to give you to transfer files " ' + + FileName ' "You accept or reject? ' ), ' file to accept ' , MB_YESNO or MB_ICONQUESTION) = ID_Yes the then // ask whether to receive 
    the begin 
//       ProgressBar1.Max: = FileSize; // initialize the progress bar 
//       ProgressBar1.Position: = 0; 
//       SaveDialog1.FileName: = FileName; // specify where to save the default file name, be sure to SaveDialog1.Execute; before, or file name is null 
//       SaveDialog1.Execute; 

      // sFile: = TFileStream.Create (SaveDialog1.FileName, fmCreate); // create a file to be written stream 
      sFile: = TFileStream. the create (FileName , fmCreate); // create a file to be written stream 
      the While FileSize> 4096  do 
      the begin 
        AThread.Connection.ReadBuffer (rbyte, 4096 ); // read the file stream 
        sFile.Write (rByte, 4096);       // write the file stream 
      @   CNT: = AThread.Connection.ReadInteger; // receive the latest location information from the transmission side progress 
@         ProgressBar1.Position: = CNT + ProgressBar1.Position; // update the progress display 
@         Label1.Caption: = 'currently received progress ..'; 
//         StatusBar1.Panels [0] .Text: = 'receiving file ...'; 
        inc is (FileSize, - 4096 );
      End ; 
      AThread.Connection.ReadBuffer (rbyte, FileSize); // .ReadBuffer (rbyte, iLen); 
      sFile.Write (rByte, FileSize); 
      sFile.Free; 
//     StatusBar1.Panels [0] .Text: = 'file receiving completion!'; 
//     Label1.Caption: = 'file receiving completion!'; 
    End ; 
   the END;

Procedure TForm1.FormCreate (Sender: TObject);
 var 
  H: TIdSocketHandle; 
the begin 
  the begin 
   IdTCPServer1.DefaultPort: = 9000 ;
 //    H: = IdTCPServer1.Bindings.Add; 
//    h.IP: = '127.0.0.1'; 
//    h.Port: = 90011; 
   IdTCPServer1.Active: = True; 
   Self.Caption: = ' start ' ;
   end ;
 end ; 

// server sends the file 
Procedure TForm1.IdTCPServer1Connect (AThread: TIdPeerThread);
 var 
  iFileHandle: Integer; 
  iFileLen, cnt: Integer; 
  buf:array[0..4096] of byte;
begin
    iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
    iFileLen:=FileSeek(iFileHandle,0,2);
    FileSeek(iFileHandle,0,0);
    AThread.Connection.WriteInteger(iFileLen);
    while true do
    begin
        cnt:=FileRead(iFileHandle,buf,4096);
        AThread.Connection.WriteBuffer(buf,cnt);
        if cnt<4096 then
            break;
    end;
    FileClose(iFileHandle);
end;

end.

Guess you like

Origin www.cnblogs.com/tobetterlife/p/12169515.html
Recommended