본문 바로가기
Delphi Tip/인터넷

Indy9 파일전송 예제

by MonoSoft 2021. 11. 24.
728x90
반응형

Indy9 파일전송 예제

 

Indy 9 버전으로 파일만 전송하는 간단한 루틴을 작성해보았습니다.

테스트된 예제입니다.

참고하시고, 즐거운 프로그래밍 하세요..

P.S : Indy10 으로 파일전송은 이 게시판에서 'Indy10' 으로

검색해보시면 찾으실 수 있습니다.

 

<Client>

<!--CodeS-->

unit uMain;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,

  StdCtrls, ComCtrls;

 

type

  TForm1 = class(TForm)

    Button1: TButton;

    IdTCPClient1: TIdTCPClient;

    ProgressBar1: TProgressBar;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

    procedure IdTCPClient1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;

      const AWorkCountMax: Integer);

    procedure IdTCPClient1Work(Sender: TObject; AWorkMode: TWorkMode;

      const AWorkCount: Integer);

    procedure IdTCPClient1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form1: TForm1;

  MaxValue : Integer;

 

implementation

{$R *.dfm}

 

procedure TForm1.IdTCPClient1WorkBegin(Sender: TObject;

  AWorkMode: TWorkMode; const AWorkCountMax: Integer);

begin

  MaxValue := AWorkCountMax;

end;

 

procedure TForm1.IdTCPClient1Work(Sender: TObject; AWorkMode: TWorkMode;

  const AWorkCount: Integer);

var

  Percent : Integer;

begin

  Percent := Round((AWorkcount / MaxValue) * 100);

  Progressbar1.Position := Percent;

  Application.ProcessMessages;

  Label1.Caption := 'Bytes 송신: '+ FormatFloat('###,#0', AWorkCount);

end;

 

procedure TForm1.IdTCPClient1WorkEnd(Sender: TObject;

  AWorkMode: TWorkMode);

begin

  Progressbar1.Position := 100;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  FileStream : TFileStream;

begin

  IdTCPClient1.Connect;

  FileStream := TFileStream.Create('c:\a.avi', fmOpenRead);

  Application.ProcessMessages;

  try

    IdTCPClient1.WriteStream(FileStream, True, True);

  finally

    FreeAndNil(FileStream);

    IdTCPClient1.DisConnect;

  end;

end;

end.

<!--CodeE-->

========================================================================

 

<Server>

<!--CodeS-->

unit uMain;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ComCtrls, IdBaseComponent, IdComponent, IdTCPServer;

 

type

  TForm1 = class(TForm)

    IdTCPServer1: TIdTCPServer;

    ProgressBar1: TProgressBar;

    Label1: TLabel;

    Memo1: TMemo;

    procedure IdTCPServer1Execute(AThread: TIdPeerThread);

    procedure IdTCPServer1Connect(AThread: TIdPeerThread);

  private

    { Private declarations }

  public

    { Public declarations }

    procedure IdTCPServerWorkBegin(Sender: TObject; AWorkMode: TWorkMode; const AWorkCountMax: Integer);

    procedure IdTCPServerWork(Sender: TObject; AWorkMode: TWorkMode; const AWorkCount: Integer);

    procedure IdTCPServerWorkend(Sender: TObject; AWorkMode: TWorkMode);

  end;

 

var

  Form1 : TForm1;

  MaxValue : Integer;

 

implementation

 

{$R *.dfm}

 

procedure TForm1.IdTCPServerWorkBegin(Sender: TObject;

AWorkMode: TWorkMode; const AWorkCountMax: Integer);

begin

  MaxValue := AWorkCountMax;

end;

 

procedure TForm1.IdTCPServerWork(Sender: TObject;

AWorkMode: TWorkMode; const AWorkCount: Integer);

var

  Percent : Integer;

begin

  Percent := Round((AWorkcount / MaxValue) * 100);

  Progressbar1.Position := Percent;

  Application.ProcessMessages;

  Label1.Caption := 'Bytes 수신: '+ FormatFloat('###,#0', AWorkCount);

end;

 

procedure TForm1.IdTCPServerWorkend(Sender: TObject; AWorkMode: TWorkMode);

begin

  Progressbar1.Position := 100;

end;

 

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

var

  FileStream : TFileStream;

begin

  FileStream := TFileStream.Create('D:\a.avi', fmCreate);

  try

    AThread.Connection.ReadStream(FileStream, -1, False);

  finally

    FreeAndNil(FileStream);

    AThread.Connection.Disconnect;

  end;

end;

 

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);

begin

  AThread.Connection.OnWork := IdTCPServerWork;

  AThread.Connection.OnWorkBegin := IdTCPServerWorkBegin;

  AThread.Connection.OnWorkEnd := IdTCPServerWorkend;

  Memo1.Lines.Add(AThread.Connection.Socket.Binding.PeerIP + ': Connecte.' );

end;

end.

<!--CodeE-->

 

728x90
반응형

댓글