T
ttily77
Unregistered / Unconfirmed
GUEST, unregistred user!
服务器端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls,Math;
type
TForm1 = class(TForm)
Button1: TButton;
IdTCPServer1: TIdTCPServer;
Edit1: TEdit;
Button3: TButton;
OpenDialog1: TOpenDialog;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
AFileStream: TFileStream; //传输的文件流
procedure CreateFileStream(filename:string) ;
public
{ Public declarations }
end;
var
Form1: TForm1;
IsUpDateOrDown :string ;
implementation
uses IdTCPConnection;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not IdTCPServer1.Active then
IdTCPServer1.Active := True;
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd,file1: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
begin
with AThread.Connection do //已经连街上的一个进程
begin
// cmd :=ReadLn(); //客户端发送的命令字符串
// if cmd = 'Down' then
// begin
// file1:=ReadLn();
CreateFileStream('F:/readrss.rar'); //创建文件流 ,(在这儿创建的话,客服各端只能收到32KB的字节)
cmd :=ReadLn();
if cmd = 'BEGIN' then //开始传输
begin
//告诉远程传输文件的大小和文件名
WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName('F:/readrss.rar')]));
Exit;
end;
if cmd = 'END' then
begin //传输完成
Exit;
end;
if cmd = 'CANCEL' then
begin //传输取消
//保持传输状态
Exit;
end;
//按照指定位置传输文件
AFileStream.Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
ASize := Min(AFileStream.Size - AFileStream.Position, RecvBufferSize);
//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream(AFileStream, false, false, ASize);
//注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
// end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//建立文件流 (在这儿 创建的话能全收到,但是我想从客服端传文件路径,这样不能满足啊,
{ AFileStream := TFileStream.Create('F:/readrss.rar', fmOpenRead);
if not IdTCPServer1.Active then IdTCPServer1.Active := True; }
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var
cmd :string;
begin
// cmd:= AThread.Connection.ReadLn() ;
ListBox1.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!')
end;
procedure TForm1.CreateFileStream(filename: string);
begin
AFileStream := TFileStream.Create(filename, fmOpenRead);
end;
end.
客服端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient,math;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
cmd: string;
ASize, TotalSize: Int64;
AFileStream: TFileStream;
begin
IdTCPClient1.Host := '192.168.0.200'; //连接主机
IdTCPClient1.Port := 8888; //端口
IdTCPClient1.Connect; //连接
try
// IdTCPClient1.WriteLn('Down');
// IdTCPClient1.WriteLn('F:/readrss.rar');
IdTCPClient1.WriteLn('BEGIN'); //提示服务器开始接收
cmd := IdTCPClient1.ReadLn();
//以“|”符号分离文件名
SaveDialog1.FileName := Copy(cmd, Pos('|', cmd) + 1, Length(cmd));
if not SaveDialog1.Execute then
begin
IdTCPClient1.WriteLn('CANCEL'); //告诉服务器取消
IdTCPClient1.Disconnect; //断开连接
exit;
end;
TotalSize := StrToInt(Copy(cmd, 0, Pos('|', cmd) - 1));
//建立文件流准备接收
AFileStream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
try //循环开始接受
repeat
IdTCPClient1.WriteLn(IntToStr(AFileStream.Size));//发送当前传输的位置
ASize := Min(TotalSize - AFileStream.Size, IdTCPClient1.RecvBufferSize);
IdTCPClient1.ReadStream(AFileStream, ASize); //接收流
Application.ProcessMessages;
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
IdTCPClient1.WriteLn('END'); //提示服务器传输完成
except
end;
end;
end.
问题是:如果我先创建好文件流的话,客户端能收到文件(但是我想要做成的是文件的路径,我想由客户端来指定),但是如果在线程中创建的话,客户端只能收到32KB的数据,请高手帮忙看看,可以另开帖给分!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls,Math;
type
TForm1 = class(TForm)
Button1: TButton;
IdTCPServer1: TIdTCPServer;
Edit1: TEdit;
Button3: TButton;
OpenDialog1: TOpenDialog;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
AFileStream: TFileStream; //传输的文件流
procedure CreateFileStream(filename:string) ;
public
{ Public declarations }
end;
var
Form1: TForm1;
IsUpDateOrDown :string ;
implementation
uses IdTCPConnection;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if not IdTCPServer1.Active then
IdTCPServer1.Active := True;
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd,file1: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
begin
with AThread.Connection do //已经连街上的一个进程
begin
// cmd :=ReadLn(); //客户端发送的命令字符串
// if cmd = 'Down' then
// begin
// file1:=ReadLn();
CreateFileStream('F:/readrss.rar'); //创建文件流 ,(在这儿创建的话,客服各端只能收到32KB的字节)
cmd :=ReadLn();
if cmd = 'BEGIN' then //开始传输
begin
//告诉远程传输文件的大小和文件名
WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName('F:/readrss.rar')]));
Exit;
end;
if cmd = 'END' then
begin //传输完成
Exit;
end;
if cmd = 'CANCEL' then
begin //传输取消
//保持传输状态
Exit;
end;
//按照指定位置传输文件
AFileStream.Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
ASize := Min(AFileStream.Size - AFileStream.Position, RecvBufferSize);
//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream(AFileStream, false, false, ASize);
//注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
// end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//建立文件流 (在这儿 创建的话能全收到,但是我想从客服端传文件路径,这样不能满足啊,
{ AFileStream := TFileStream.Create('F:/readrss.rar', fmOpenRead);
if not IdTCPServer1.Active then IdTCPServer1.Active := True; }
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var
cmd :string;
begin
// cmd:= AThread.Connection.ReadLn() ;
ListBox1.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!')
end;
procedure TForm1.CreateFileStream(filename: string);
begin
AFileStream := TFileStream.Create(filename, fmOpenRead);
end;
end.
客服端:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient,math;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
cmd: string;
ASize, TotalSize: Int64;
AFileStream: TFileStream;
begin
IdTCPClient1.Host := '192.168.0.200'; //连接主机
IdTCPClient1.Port := 8888; //端口
IdTCPClient1.Connect; //连接
try
// IdTCPClient1.WriteLn('Down');
// IdTCPClient1.WriteLn('F:/readrss.rar');
IdTCPClient1.WriteLn('BEGIN'); //提示服务器开始接收
cmd := IdTCPClient1.ReadLn();
//以“|”符号分离文件名
SaveDialog1.FileName := Copy(cmd, Pos('|', cmd) + 1, Length(cmd));
if not SaveDialog1.Execute then
begin
IdTCPClient1.WriteLn('CANCEL'); //告诉服务器取消
IdTCPClient1.Disconnect; //断开连接
exit;
end;
TotalSize := StrToInt(Copy(cmd, 0, Pos('|', cmd) - 1));
//建立文件流准备接收
AFileStream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
try //循环开始接受
repeat
IdTCPClient1.WriteLn(IntToStr(AFileStream.Size));//发送当前传输的位置
ASize := Min(TotalSize - AFileStream.Size, IdTCPClient1.RecvBufferSize);
IdTCPClient1.ReadStream(AFileStream, ASize); //接收流
Application.ProcessMessages;
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
IdTCPClient1.WriteLn('END'); //提示服务器传输完成
except
end;
end;
end.
问题是:如果我先创建好文件流的话,客户端能收到文件(但是我想要做成的是文件的路径,我想由客户端来指定),但是如果在线程中创建的话,客户端只能收到32KB的数据,请高手帮忙看看,可以另开帖给分!