现在是这样的:
procedure TForm1.ServerExecute(AThread: TIdPeerThread);
var
cmd: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
FileName:string;
OSVersion:integer;
path:string;
ip:string;
VersionPath: string; //文件路径,在各版本的文件夹下
begin
ip := AThread.Connection.Socket.Binding.PeerIP;
with AThread.Connection do
begin
cmd := UpperCase(ReadLn);
if LeftStr(cmd,5) = 'BEGIN' then //开始传输 cmd命令格式(BEGIN:操作系统类型(两位数字):文件名)
begin
OSVersion := strtoint(MidStr(cmd, 7, 2)); //OSVersion格式(XP以3开头,如31,33)
case OSVersion of
31: VersionPath := 'XPsp1';
32: VersionPath := 'XPsp2';
33: VersionPath := 'XPsp3';
41: VerSionPath := 'Vista';
51: VerSionPath := 'Win7';
62: VerSionPath := '2003sp2';
//...
end;
FileName := MidStr(cmd, 10, 128);
tdlcslog('3:'+ip+' 请求文件名为 ' + VersionPath + '\' + FileName);
Path := ExtractFilePath(Application.ExeName)
+ 'SysFile\' + VersionPath + '\'+FileName;
if FileExists(Path) then
begin
try
FileStream := TFileStream.Create(Path,fmOpenRead);//全局变量
//告诉远程传输文件的大小和文件名
WriteLn(inttostr(FileStream.Size)+'|'+FileName);
tdlcslog('4:'+ip+' 文件大小 ' + inttostr(FileStream.Size)+'|'+FileName);
//ProgressBar1.Max := FileStream.Size;
//ProgressBar1.Position := 0;
except
end;
end else
begin
WriteLn('-1');
tdlcslog('4:'+ip+' 文件大小 -1, 文件不存在');
end;
Exit;
end; //end of if cmd = 'BEGIN'
if cmd = 'END' then
begin
tdlcslog('100:'+ip+' 传输完成');
FileStream.Free;
//AThread.Stop;
Exit;
end;
if cmd = 'CANCEL' then
begin
tdlcslog('99:'+ip+' 传输取消');
FileStream.Free;
//AThread.Stop;
Exit;
end;
//按照指定位置传输文件
FileStream.Seek(StrToInt(cmd), soFromBeginning); //转到文件流传输的位置
ASize := Min(FileStream.Size - FileStream.Position, RecvBufferSize);//计算需要发送的大小,Min()函数在Math单元
OpenWriteBuffer; //准备发送缓冲
WriteStream(FileStream, false, false, ASize); //注意这个函数的参数。
CloseWriteBuffer; //结束发送缓冲
//StatusBar1.SimpleText := Format('当前传输位置%d/大小%d', [strtoint(cmd)+ASize, FileStream.Size]);
//ProgressBar1.Position := ProgressBar1.Position + ASize;
end; //end of with AThread.Connection
end;