现在是这样的: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;
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;
解决方案 »
- dll中返回pchar值,但调用放不确定返回大小,怎么申请内存?
- delphi 把exe转换成ocx
- 字符串处理问题~头大~内详,请各位大侠帮忙。。救命~
- 反HOOK
- 有朋友可以帮我解决一个这样的问题吗。高分送!!!!!!
- 有一些word文档,图片,想做成多媒体光盘型式,用什么工具比较好,推荐几个
- 有变量n,怎么样动态的建n个label?
- 对于paradox中Memo型的字段怎样用sql语句查询
- ◆◆ 招 聘!! ◆◆
- 关于 tcollection ,tcollectionitem,的使用?
- TShellWindows 获取当前浏览器URl问题
- delphi 写一个下载程序,就像下载游戏前的那个下载软件,怎么样得到下载速率?
现在是(仅)能与一个客户端正常传输文件,我这个服务端必须要支持多个客户端(比如10个就够了),这个不知怎么实现?
procedure TForm1.ServerExecute(AThread: TIdPeerThread);
var
cmd: string; //接收到客户端的字符串信息
ASize: Integer; //需要传输的流大小
FileName:string;
OSVersion:integer;
path:string;
ip:string;
VersionPath: string; //文件路径,在各版本的文件夹下
FileStream: TFileStream; //传输的文件流
begin ip := AThread.Connection.Socket.Binding.PeerIP;
//tdlcslog('ee:'+ip+' 进入ServerExecute。 ');
with AThread.Connection do
begin
cmd := UpperCase(ReadLn); try 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
FileStream := TFileStream.Create(Path,fmOpenRead);
//告诉远程传输文件的大小和文件名
WriteLn(inttostr(FileStream.Size)+'|'+FileName);
tdlcslog('4:'+ip+' 文件大小 ' + inttostr(FileStream.Size)+'|'+FileName); //AThread.Data := Pointer( client ); //指到 athread的指针中
//ProgressBar1.Max := FileStream.Size;
//ProgressBar1.Position := 0;
end else
begin
WriteLn('-1');
tdlcslog('4:'+ip+' 文件大小 -1, 文件不存在');
exit;
end; //传文件的内容
while true do
begin
cmd := ReadLn;
if UpperCase(cmd) = 'END' then
begin
tdlcslog('100:'+ip+' 传输完成');
FileStream.Free;
break;
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; //end of if cmd = 'BEGIN' except
end;
end; //end of with AThread.Connection
end;另
加个TIdThreadMgrDefault控件与TidTcpServer.ThreadMgr关联,管理TIdPeerThread。