D
doglive
Unregistered / Unconfirmed
GUEST, unregistred user!
用TIdTCPClient;和TIdTCPServer来传输文件
//服务器端代码如下:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
//AFileStream : Tfilestream ;
begin
with AThread.Connection do //已经连街上的一个进程
begin
cmd := UpperCase(ReadLn); //客户端发送的命令字符串
if cmd = 'BEGIN' then //开始传输
begin
//告诉远程传输文件的大小和文件名
WriteLn(Format('%d|%s', [(Athread.Data as Tfilestream).Size, ExtractFileName(Edit1.Text)]));
StatusBar1.SimpleText := '准备传输...';
Exit;
end;
if cmd = 'END' then
begin //传输完成
Button3.Click;
StatusBar1.SimpleText := '传输完成...';
Exit;
end;
if cmd = 'CANCEL' then
begin //传输取消
StatusBar1.SimpleText := '传输取消...';
//保持传输状态
Exit;
end;
//按照指定位置传输文件
//cmd := UpperCase(ReadLn('CANCEL',1000,32767)) ;
while (cmd<>'END') and (cmd <> 'CANCEL') do
begin
(Athread.Data as Tfilestream).Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
ASize := Min((Athread.Data as Tfilestream).Size - (Athread.Data as Tfilestream).Position, RecvBufferSize);
//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream((Athread.Data as Tfilestream), false, false, ASize);
//注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
StatusBar1.SimpleText := Format('当前传输位置%s/大小%d', [cmd, (Athread.Data as Tfilestream).Size]);
ProgressBar1.Position := ProgressBar1.Position + ASize;
cmd := UpperCase(ReadLn); //客户端发送的命令字符串
end ;
//Button3.Click;
StatusBar1.SimpleText := '传输完成...';
end;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var AFileStream : Tfilestream ;
begin
AFileStream := TFileStream.Create(Edit1.Text, fmOpenRead);
Athread.Data := AfileStream ;
end;
//客户端代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
cmd: string;
ASize, TotalSize: Int64;
AFileStream: TFileStream;
begin
IdTCPClient1.Host := Edit1.Text; //连接主机
IdTCPClient1.Port := StrToIntDef(Edit2.Text, 9925); //端口
IdTCPClient1.Connect ; //连接
try
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);
//showmessage('IdTcpClient1.RecvBuffersize='+inttostr(IdTcpClient1.RecvBufferSize));
edit6.Text:=inttostr(IdTcpClient1.RecvBufferSize);
//showmessage('TotalSize='+inttostr(TotalSize));
edit7.Text:=inttostr(TotalSize);
//showmessage('AFileStream.Size='+inttostr(AFileStream.Size));
edit8.Text:=inttostr(AFileStream.Size);
//showmessage('Asize='+inttostr(ASize));
edit9.Text:=inttostr(ASize);
//选择剩余大小和缓冲区大小小的一个作为传输的大小
IdTCPClient1.ReadStream(AFileStream, ASize); //接收流
//showmessage('AFileStream.size'+inttostr(AFileStream.size));
edit10.Text:=inttostr(AFileStream.size);
//showmessage('Asize='+inttostr(ASize));
StatusBar1.SimpleText := Format('当前传输位置%d/大小%d', [AFileStream.Size, TotalSize]);
Application.ProcessMessages;
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
IdTCPClient1.WriteLn('END'); //提示服务器传输完成
StatusBar1.SimpleText := '传输完成...';
except
StatusBar1.SimpleText := '连接服务器失败或者对方已经中断传输!';
end;
IdTCPClient1.Disconnect;
end;
为什么不能启动多个客户端呢,只能向一个客户端传输文件,请高手指教
//服务器端代码如下:
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
//AFileStream : Tfilestream ;
begin
with AThread.Connection do //已经连街上的一个进程
begin
cmd := UpperCase(ReadLn); //客户端发送的命令字符串
if cmd = 'BEGIN' then //开始传输
begin
//告诉远程传输文件的大小和文件名
WriteLn(Format('%d|%s', [(Athread.Data as Tfilestream).Size, ExtractFileName(Edit1.Text)]));
StatusBar1.SimpleText := '准备传输...';
Exit;
end;
if cmd = 'END' then
begin //传输完成
Button3.Click;
StatusBar1.SimpleText := '传输完成...';
Exit;
end;
if cmd = 'CANCEL' then
begin //传输取消
StatusBar1.SimpleText := '传输取消...';
//保持传输状态
Exit;
end;
//按照指定位置传输文件
//cmd := UpperCase(ReadLn('CANCEL',1000,32767)) ;
while (cmd<>'END') and (cmd <> 'CANCEL') do
begin
(Athread.Data as Tfilestream).Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
ASize := Min((Athread.Data as Tfilestream).Size - (Athread.Data as Tfilestream).Position, RecvBufferSize);
//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream((Athread.Data as Tfilestream), false, false, ASize);
//注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
StatusBar1.SimpleText := Format('当前传输位置%s/大小%d', [cmd, (Athread.Data as Tfilestream).Size]);
ProgressBar1.Position := ProgressBar1.Position + ASize;
cmd := UpperCase(ReadLn); //客户端发送的命令字符串
end ;
//Button3.Click;
StatusBar1.SimpleText := '传输完成...';
end;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var AFileStream : Tfilestream ;
begin
AFileStream := TFileStream.Create(Edit1.Text, fmOpenRead);
Athread.Data := AfileStream ;
end;
//客户端代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
cmd: string;
ASize, TotalSize: Int64;
AFileStream: TFileStream;
begin
IdTCPClient1.Host := Edit1.Text; //连接主机
IdTCPClient1.Port := StrToIntDef(Edit2.Text, 9925); //端口
IdTCPClient1.Connect ; //连接
try
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);
//showmessage('IdTcpClient1.RecvBuffersize='+inttostr(IdTcpClient1.RecvBufferSize));
edit6.Text:=inttostr(IdTcpClient1.RecvBufferSize);
//showmessage('TotalSize='+inttostr(TotalSize));
edit7.Text:=inttostr(TotalSize);
//showmessage('AFileStream.Size='+inttostr(AFileStream.Size));
edit8.Text:=inttostr(AFileStream.Size);
//showmessage('Asize='+inttostr(ASize));
edit9.Text:=inttostr(ASize);
//选择剩余大小和缓冲区大小小的一个作为传输的大小
IdTCPClient1.ReadStream(AFileStream, ASize); //接收流
//showmessage('AFileStream.size'+inttostr(AFileStream.size));
edit10.Text:=inttostr(AFileStream.size);
//showmessage('Asize='+inttostr(ASize));
StatusBar1.SimpleText := Format('当前传输位置%d/大小%d', [AFileStream.Size, TotalSize]);
Application.ProcessMessages;
until AFileStream.Size = TotalSize; //大小一致了表示结束
finally
AFileStream.Free; //释放文件流
end;
IdTCPClient1.WriteLn('END'); //提示服务器传输完成
StatusBar1.SimpleText := '传输完成...';
except
StatusBar1.SimpleText := '连接服务器失败或者对方已经中断传输!';
end;
IdTCPClient1.Disconnect;
end;
为什么不能启动多个客户端呢,只能向一个客户端传输文件,请高手指教