D
dh9450
Unregistered / Unconfirmed
GUEST, unregistred user!
在网上找了一个例子 但是只能一对一的传
如果 两个Client同时往Server传收到的文件会有问题
偶需要 大概 30个客户端同时往一个服务器传文件的
是不是要分线程啊
哪位有好办法吗
那个代码如下
---------------------
Client:
Var ReceivedString:String;
Mem:TFileStream;
p:Array[0..1023] of byte;
Posi,Len:Integer;
begin
if OpenDialog1.Execute then
begin
IdUDPClient1.Host:=Edit1.Text;
IdUDPClient1.Active:=True;
IdUDPClient1.Send('Send file:File Name:'+OpenDialog1.FileName);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE NAME OK!' then
begin
Mem:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
try
Posi:=0;
IdUDPClient1.Send('Send File:File Length:'+IntToStr(Mem.Size));
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE LENGTH OK!' then
begin
While Posi<Mem.Size do//一次只发1024个字节,字节数不能太多,不过应该还可以增加一些.
begin
Len:=1024;
if Mem.Size-Posi<1024 then
Len:=Mem.Size-Posi;
Mem.Read(p,Len);
IdUDPClient1.SendBuffer(P,Len);
Inc(Posi,Len);
Gauge1.Progress:=Round(Posi/Mem.Size*100);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)<>'RECIVED FILE PACKAGE OK!' then
Break;
Application.ProcessMessages;
end;
IdUDPClient1.Send('Send File:File End!');
end else
ShowMessage('Send file cancel!');
finally
Mem.Free;
end;
end;
end;
end;
Server:
var FileName:string;
FileSize:integer;
Mem:TFileStream;
//这3个是单元变量 偶觉得问题就在这
Var Str:String;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Pos('Send file:File Name:',Str)>0 then
begin
Delete(Str,1,Length('Send file:File Name:'));
FileName:=Str;
Str:='Recived File Name OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else if Pos('Send File:File Length:',Str)>0 then
begin
Delete(Str,1,Length('Send File:File Length:'));
FileSize:=StrToIntDef(Str,0);
SaveDialog1.FileName:=FileName;
if SaveDialog1.Execute then
begin
FileName:=SaveDialog1.FileName;
if FileExists(FileName) then
DeleteFile(FileName);
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
end;
if not FileExists(FileName) then
Mem:=TFileStream.Create(FileName,fmOpenReadWrite or fmCreate)
else
Mem:=TFileStream.Create(FileName,fmOpenReadWrite);
Str:='Recived File Length OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else
begin
Str:='Recived File Length Cancel!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
end else if Pos('Send File:File End!',Str)>0 then
begin
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
Str:='Recived File OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
FileName:='';
FileSize:=0;
end
else
begin
if Mem<>nil then
begin
Mem.Seek(0,2);
AData.Seek(0,0);
Mem.CopyFrom(AData,AData.Size);
Gauge1.Progress:=Round(Mem.Size/FileSize*100);
Str:='Recived File Package OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
end;
---------------------
如果 两个Client同时往Server传收到的文件会有问题
偶需要 大概 30个客户端同时往一个服务器传文件的
是不是要分线程啊
哪位有好办法吗
那个代码如下
---------------------
Client:
Var ReceivedString:String;
Mem:TFileStream;
p:Array[0..1023] of byte;
Posi,Len:Integer;
begin
if OpenDialog1.Execute then
begin
IdUDPClient1.Host:=Edit1.Text;
IdUDPClient1.Active:=True;
IdUDPClient1.Send('Send file:File Name:'+OpenDialog1.FileName);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE NAME OK!' then
begin
Mem:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
try
Posi:=0;
IdUDPClient1.Send('Send File:File Length:'+IntToStr(Mem.Size));
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)='RECIVED FILE LENGTH OK!' then
begin
While Posi<Mem.Size do//一次只发1024个字节,字节数不能太多,不过应该还可以增加一些.
begin
Len:=1024;
if Mem.Size-Posi<1024 then
Len:=Mem.Size-Posi;
Mem.Read(p,Len);
IdUDPClient1.SendBuffer(P,Len);
Inc(Posi,Len);
Gauge1.Progress:=Round(Posi/Mem.Size*100);
ReceivedString := IdUDPClient1.ReceiveString();
if UpperCase(ReceivedString)<>'RECIVED FILE PACKAGE OK!' then
Break;
Application.ProcessMessages;
end;
IdUDPClient1.Send('Send File:File End!');
end else
ShowMessage('Send file cancel!');
finally
Mem.Free;
end;
end;
end;
end;
Server:
var FileName:string;
FileSize:integer;
Mem:TFileStream;
//这3个是单元变量 偶觉得问题就在这
Var Str:String;
begin
AData.Seek(0,0);
SetLength(Str,AData.Size);
AData.Read(Str[1],AData.Size);
if Pos('Send file:File Name:',Str)>0 then
begin
Delete(Str,1,Length('Send file:File Name:'));
FileName:=Str;
Str:='Recived File Name OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else if Pos('Send File:File Length:',Str)>0 then
begin
Delete(Str,1,Length('Send File:File Length:'));
FileSize:=StrToIntDef(Str,0);
SaveDialog1.FileName:=FileName;
if SaveDialog1.Execute then
begin
FileName:=SaveDialog1.FileName;
if FileExists(FileName) then
DeleteFile(FileName);
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
end;
if not FileExists(FileName) then
Mem:=TFileStream.Create(FileName,fmOpenReadWrite or fmCreate)
else
Mem:=TFileStream.Create(FileName,fmOpenReadWrite);
Str:='Recived File Length OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end else
begin
Str:='Recived File Length Cancel!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
end else if Pos('Send File:File End!',Str)>0 then
begin
if Mem<>nil then
begin
Mem.Free;
Mem:=nil;
Str:='Recived File OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
end;
FileName:='';
FileSize:=0;
end
else
begin
if Mem<>nil then
begin
Mem.Seek(0,2);
AData.Seek(0,0);
Mem.CopyFrom(AData,AData.Size);
Gauge1.Progress:=Round(Mem.Size/FileSize*100);
Str:='Recived File Package OK!';
ABinding.SendTo(ABinding.PeerIP, ABinding.PeerPort, Str[1], Length(Str));
Application.ProcessMessages;
end;
end;
end;
---------------------