IdTCPClient传输文件出错
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ComCtrls;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
IdTCPClient1: TIdTCPClient;
Button1: TButton;
edtFileName: TEdit;
edtAddress: TEdit;
Button2: TButton;
ProgressBar1: TProgressBar;
StatusBar1: TStatusBar;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
edtFileName.Text:=OpenDialog1.FileName
end;
procedure TForm1.Button2Click(Sender: TObject);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
if (edtAddress.Text<>'')and (edtFileName.Text<>'') then
begin
IdTCPClient1.Host:=edtAddress.Text;
IdTCPClient1.Port:=9925;
try
IdTCPClient1.Connect(5000);
except
StatusBar1.Panels[0].Text:='连接接受方失败!';
exit;
end;
if IdTCPClient1.Connected then
begin
iFileHandle:=FileOpen(edtFileName.Text,fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
ProgressBar1.Max:=iFileLen;
ProgressBar1.Position := 0;
IdTCPClient1.WriteLn(ExtractFileName(edtFileName.Text)+'|'+IntToStr(iFileLen));
while true do
begin
Application.ProcessMessages;
cnt:=FileRead(iFileHandle,buf,4096);
IdTCPClient1.WriteBuffer(buf,cnt);
IdTCPClient1.WriteInteger(cnt);
ProgressBar1.Position:=ProgressBar1.Position + cnt;
StatusBar1.Panels[0].Text:='正在传送文件...';
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
Label2.Caption:='文件传送完成!';
StatusBar1.Panels[0].Text:='文件传送完成!';
end;
end
else
ShowMessage('请选择要传送的文件和或接受方地址');
end;
end.
错误:list index out of bounds(0)