indy 文件传输的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 jzg2727
  • 开始时间 开始时间
J

jzg2727

Unregistered / Unconfirmed
GUEST, unregistred user!
我在网上下载了个文件传输的例程,使用的是indy的控件,如果我不把接收端打开的时候,用发送端发送文件时就出现
socket error # 10061
connection refused 的错误提示,我该如果改动程序才能使发送端在发送文件时,如果接收端没有打开或者网络不通时,不发送文件,也不弹出报错的提示框
请帮助。
源码如下:
发送端:

unit uClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, ComCtrls, StdCtrls, ExtDlgs,jpeg;

type
TfmClient = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
ProgressBar1: TProgressBar;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fmClient: TfmClient;

implementation

{$R *.dfm}

procedure TfmClient.Button1Click(Sender: TObject);
var
Buf : array[0..1023] of Byte;
ReadCount : Integer;
aStream : TFileStream;
aSize : Integer;
aFileName : String;
begin
if OpenDialog1.Execute then
begin
IdTCPClient1.Connect;
aStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyWrite);
try
//发送文件名
aFileName := ExtractFileName(OpenDialog1.FileName);
IdTCPClient1.WriteLn(aFileName);
//等待接受确认
IdTCPClient1.ReadLn(#13#10, 1000);
//写文件长度和文件流
aSize := aStream.Size;
ProgressBar1.Max := aSize;
IdTCPClient1.WriteBuffer(aSize, 4);
//IdTCPClient1.WriteStream(aStream);
while aStream.Position < aStream.Size do
begin
if aStream.Size - aStream.Position >= SizeOf(Buf) then
ReadCount := sizeOf(Buf)
else
ReadCount := aStream.Size - aStream.Position;
aStream.ReadBuffer(Buf, ReadCount);
IdTCPClient1.WriteBuffer(Buf, ReadCount);
ProgressBar1.Position := aStream.Position;
Application.ProcessMessages;
end;
Caption := IdTCPClient1.ReadLn;
IdTCPClient1.Disconnect;
finally
aStream.Free;
end;
end;

end;

end.

接收端:

unit uServer;

interface

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

type
TDataState = (dstNone, dstReceiving);
//Data对象用来保存一个连接的状态以及一些变量
TThreadData = class
private
FState: TDataState;
FFileSize: Integer;
FStream: TFileStream;
procedure SetState(const Value: TDataState);
procedure SetFileSize(const Value: Integer);
procedure SetStream(const Value: TFileStream);
public
constructor Create;
destructor Destroy; override;
property State : TDataState read FState write SetState;
property FileSize : Integer read FFileSize write SetFileSize;
property Stream : TFileStream read FStream write SetStream;
end;

type
TfmServer = class(TForm)
IdTCPServer1: TIdTCPServer;
ProgressBar1: TProgressBar;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fmServer: TfmServer;

implementation

{$R *.dfm}

procedure TfmServer.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Data := TThreadData.Create;
with AThread.Data as TThreadData do
begin
State := dstNone;
end;
end;

procedure TfmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
aFileSize : Integer;
aFileName : String;
Buff : array[0..1023] of Byte;
ReadCount : Integer;
begin
with AThread.Data as TThreadData do
begin
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件名
aFileName := AThread.Connection.ReadLn(#13#10, 100);
if aFileName = '' then
Exit;


//返回确认文件传输标志
AThread.Connection.WriteLn;
//开始读取文件长度,创建文件
AThread.Connection.ReadBuffer(aFileSize, 4);
FileSize := aFileSize;
ProgressBar1.Max := FileSize;
Stream := TFileStream.Create(aFileName, fmCreate);
State := dstReceiving;
end

end;


if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if FileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(Buff, ReadCount);
Stream.WriteBuffer(Buff, ReadCount);
ProgressBar1.Position := Stream.Size;
Caption := IntToStr(Stream.Size) + '/' + IntToStr(FileSize);
Application.ProcessMessages;
until Stream.Size >= FileSize;
AThread.Connection.WriteLn('OK');
Stream.Free;
Stream := nil;
State := dstNone;
end;
end;
end;

{ TThreadData }

constructor TThreadData.Create;
begin
inherited;
Stream := nil;
end;

destructor TThreadData.Destroy;
begin
if Assigned(Stream) then
Stream.Free;
inherited;
end;

procedure TThreadData.SetFileSize(const Value: Integer);
begin
FFileSize := Value;
end;

procedure TThreadData.SetState(const Value: TDataState);
begin
FState := Value;
end;

procedure TThreadData.SetStream(const Value: TFileStream);
begin
FStream := Value;
end;

end.
 
我在网上下载了个文件传输的例程,使用的是indy的控件,如果我不把接收端打开的时候,用发送端发送文件时就出现
socket error # 10061
connection refused 的错误提示,我该如果改动程序才能使发送端在发送文件时,如果接收端没有打开或者网络不通时,不发送文件,也不弹出报错的提示框
请帮助。
源码如下:
发送端:

unit uClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, ComCtrls, StdCtrls, ExtDlgs,jpeg;

type
TfmClient = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
ProgressBar1: TProgressBar;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fmClient: TfmClient;

implementation

{$R *.dfm}

procedure TfmClient.Button1Click(Sender: TObject);
var
Buf : array[0..1023] of Byte;
ReadCount : Integer;
aStream : TFileStream;
aSize : Integer;
aFileName : String;
begin
if OpenDialog1.Execute then
begin
IdTCPClient1.Connect;
aStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyWrite);
try
//发送文件名
aFileName := ExtractFileName(OpenDialog1.FileName);
IdTCPClient1.WriteLn(aFileName);
//等待接受确认
IdTCPClient1.ReadLn(#13#10, 1000);
//写文件长度和文件流
aSize := aStream.Size;
ProgressBar1.Max := aSize;
IdTCPClient1.WriteBuffer(aSize, 4);
//IdTCPClient1.WriteStream(aStream);
while aStream.Position < aStream.Size do
begin
if aStream.Size - aStream.Position >= SizeOf(Buf) then
ReadCount := sizeOf(Buf)
else
ReadCount := aStream.Size - aStream.Position;
aStream.ReadBuffer(Buf, ReadCount);
IdTCPClient1.WriteBuffer(Buf, ReadCount);
ProgressBar1.Position := aStream.Position;
Application.ProcessMessages;
end;
Caption := IdTCPClient1.ReadLn;
IdTCPClient1.Disconnect;
finally
aStream.Free;
end;
end;

end;

end.

接收端:

unit uServer;

interface

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

type
TDataState = (dstNone, dstReceiving);
//Data对象用来保存一个连接的状态以及一些变量
TThreadData = class
private
FState: TDataState;
FFileSize: Integer;
FStream: TFileStream;
procedure SetState(const Value: TDataState);
procedure SetFileSize(const Value: Integer);
procedure SetStream(const Value: TFileStream);
public
constructor Create;
destructor Destroy; override;
property State : TDataState read FState write SetState;
property FileSize : Integer read FFileSize write SetFileSize;
property Stream : TFileStream read FStream write SetStream;
end;

type
TfmServer = class(TForm)
IdTCPServer1: TIdTCPServer;
ProgressBar1: TProgressBar;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;

var
fmServer: TfmServer;

implementation

{$R *.dfm}

procedure TfmServer.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
AThread.Data := TThreadData.Create;
with AThread.Data as TThreadData do
begin
State := dstNone;
end;
end;

procedure TfmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
aFileSize : Integer;
aFileName : String;
Buff : array[0..1023] of Byte;
ReadCount : Integer;
begin
with AThread.Data as TThreadData do
begin
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件名
aFileName := AThread.Connection.ReadLn(#13#10, 100);
if aFileName = '' then
Exit;


//返回确认文件传输标志
AThread.Connection.WriteLn;
//开始读取文件长度,创建文件
AThread.Connection.ReadBuffer(aFileSize, 4);
FileSize := aFileSize;
ProgressBar1.Max := FileSize;
Stream := TFileStream.Create(aFileName, fmCreate);
State := dstReceiving;
end

end;


if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if FileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(Buff, ReadCount);
Stream.WriteBuffer(Buff, ReadCount);
ProgressBar1.Position := Stream.Size;
Caption := IntToStr(Stream.Size) + '/' + IntToStr(FileSize);
Application.ProcessMessages;
until Stream.Size >= FileSize;
AThread.Connection.WriteLn('OK');
Stream.Free;
Stream := nil;
State := dstNone;
end;
end;
end;

{ TThreadData }

constructor TThreadData.Create;
begin
inherited;
Stream := nil;
end;

destructor TThreadData.Destroy;
begin
if Assigned(Stream) then
Stream.Free;
inherited;
end;

procedure TThreadData.SetFileSize(const Value: Integer);
begin
FFileSize := Value;
end;

procedure TThreadData.SetState(const Value: TDataState);
begin
FState := Value;
end;

procedure TThreadData.SetStream(const Value: TFileStream);
begin
FStream := Value;
end;

end.
 
再加上try...except不就可以了?
 
对连接时进行TRY... EXCEPT处理
try
IdTCPClient1.Connect;
except
exit;
 
谢谢了,解决了
 
后退
顶部