5,939
社区成员
发帖
与我相关
我的任务
分享
Delphi(Pascal) code
private
{ Private declarations }
AFileStream: TFileStream; //传输的文件流
procedure Tf_main.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
with AThread.Connection do
begin
sCommand := ReadLn();
if AnsiStartsText('down@@',sCommand) then
begin
filepath:='f:\共公盘\'+copy(sCommand,Pos('@@',sCommand)+2,length(sCommand));
AFileStream := TFileStream.Create(filepath, fmOpenRead);
end;
if sCommand = 'BEGIN' then //开始传输
begin
//告诉远程传输文件的大小和文件名
WriteLn(Format('%d|%s', [AFileStream.Size,'xxx']));
Exit;
end;
//按照指定位置传输文件
AFileStream.Seek(StrToInT(sCommand), soFromBeginning); //转到文件流传输的位置
ASize := Min(AFileStream.Size - AFileStream.Position, RecvBufferSize);
//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream(AFileStream, false, false, ASize);
//注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
end;
end;