我现在做一个基于P2P的网络文件系统,可是我发现我用数组来存文件的数据的时候会出现性能问题,所以我想改用要用的时候再来取,就是用TFileStream来读取文件的一小部分放到临时的分配空间里面,再发到请求的对方,然后写入文件,可是数据发到对方的时候出现了问题,不知道哪里出现,请你们帮我看看。
数据定义:
//发送的文件数据基本数据结构
type RTranFileBlockData=packed record
     Account:array [0..20] of Char;      {用户ID}
     IpAddr:array[0..20] of char;
     Prot: array[0..5] of char;
     BlockNo:array[0..5] of char;
     BlockCount:array[0..5] of Char;
     ConfirmNo:array[0..5] of char;
     FileName:array[0..100] of char;
     FBlockData:array[0..1023] of byte;
     CRC:array[0..10] of char;
end;
//文件数据块的发送数据包
type PTranFileBlockData= packed record
     header:RHeader;
     body  :RTranFileBlockData;
end;//以下是数据操作
//读取小块文件数据
procedure readFileBlcokData(FileDir:string;BlockNo:integer);
begin
    form1.Memo1.Lines.Add('读文件['+FileDir+']的数据块['+inttostr(BlockNo)+']');
    setlength(FBlockBufSed,0);
    setlength(FBlockBufSed,1023);
    sFileMem:=TFileStream.Create(FileDir,fmOpenRead);
    //sfilemem.Position:=BlockNo*1024;
    form1.Memo1.Lines.Add('读前BLockNo:'+inttostr(BlockNo));
    sFilemem.Seek(BlockNo*1024,soFromBeginning);    sFilemem.Read(FBlockBufSed[0],1024);
    sFilemem.Free;
end;
//把小块数据写入文件
procedure WriteFileBlockData(FileDir:string;BlockNo,BlockSize:integer);
var
    i:integer;
begin
    form1.Memo1.Lines.Add('写文件['+FileDir+']的数据块['+inttostr(BlockNo)+']大小为['+inttostr(BlockSize)+']');
    if fileexists(FileDir) then
    begin
         rFileMem:=TFileStream.Create(FileDir,fmOpenReadWrite);
    end
    else
    begin
          rFileMem:=TFileStream.Create(FileDir, fmCreate);
    end;    //rfilemem.Position:=BlockNo*1024;
    rFilemem.Seek(BlockNo*1024,soFromBeginning);
   //showmessage(inttostr(BlockNo));
   form1.Memo1.Lines.Add('写前BLockNo:'+inttostr(BlockNo));
    rFilemem.Write(FBlockBufSed[0],1024);
    rFilemem.Free;
end;//处理发送数据文件块
procedure ProcRecvResDLFileBlockData(AData: TStream);
var
    _PTranFileBlockData:PTranFileBlockData;
    _RTranFileBlockData:RTranFileBlockData;
    FilePath:string;
    i:integer;
begin
      //saldfa;sd
      adata.ReadBuffer(_RTranFileBlockData,sizeof(_RTranFileBlockData));
      //合成文件路径
      FilePath:=FileShareDir+_RTranFileBlockData.FileName;
      if FileExists(FilePath)then
      begin
           readFileBlcokData(FilePath,strtoint(_RTranFileBlockData.BlockNo));
      end;
      for i:=0 to 1023 do
      begin
      _RTranFileBlockData.FBlockData[i]:=FBlockBufSed[i];
      end;
      _PTranFileBlockData.header.InfoType:='83';
      strpcopy(_PTranFileBlockData.body.Account,cliAccount);
      strpcopy(_PTranFileBlockData.body.IpAddr,PublicIP);
      strpcopy(_PTranFileBlockData.body.Prot,inttostr(runPort));
      strpcopy(_PTranFileBlockData.body.BlockNo,_RTranFileBlockData.BlockNo);
      strpcopy(_PTranFileBlockData.body.ConfirmNo,'0');
      strpcopy(_PTranFileBlockData.body.FileName,trim(_RTranFileBlockData.FileName));
      form1.PpShareCli.SendBuffer(_RTranFileBlockData.IpAddr,strtoint(_RTranFileBlockData.Prot),_PTranFileBlockData,sizeof(_PTranFileBlockData));
end;///
//收到要下载文件的数据块
procedure ProcRecvFileBlockData(AData: TStream);
var
_RTranFileBlockData:RTranFileBlockData;
_PTranFileBlockData:PTranFileBlockData;
FileName,bno:string;
i:integer;
begin
     adata.ReadBuffer(_RTranFileBlockData,sizeof(_RTranFileBlockData));
      setlength(FBlockBufSed,0);
     setlength(FBlockBufSed,1024);
     for i:=0 to 1023 do
     begin
         FBlockBufSed[i]:= _rTranFileBlockData.FBlockData[i];
     end;
     FileName:='c:\myPPShare\'+ExtractFileName(_rTranFileBlockData.FileName);
     WriteFileBlockData(FileName,strtoint(_rTranFileBlockData.BlockNo),sizeof(_rTranFileBlockData.FBlockData));
     form1.Memo1.Lines.Add('数据已经写入文件了。');
     if (strtoint(_RTranFileBlockData.BlockNo)<BlockCount-1) then
     begin
         //发送请求下一块数据
         form1.Memo1.Lines.Add('再次发送请求前:BLockNo为:'+_RTranFileBlockData.BlockNo+'--BlockCount为:'+inttostr(BlockCount));
         bno:=_RTranFileBlockData.BlockNo;
         bno:=inttostr(strtoint(bno)+1);
          _PTranFileBlockData.header.InfoType:='81';
          strpcopy(_PTranFileBlockData.body.BlockNo,bno);
          strpcopy(_PTranFileBlockData.body.Account,cliAccount);
          strpcopy(_PTranFileBlockData.body.IpAddr,PublicIP);
          strpcopy(_PTranFileBlockData.body.Prot,inttostr(runPort));
          strpcopy(_PTranFileBlockData.body.ConfirmNo,'-1');
          //strpcopy(_RTranFileBlockData.FBlockData,'');
          strpcopy(_PTranFileBlockData.body.FileName,trim(_RTranFileBlockData.FileName));
          form1.PpShareCli.SendBuffer(_RTranFileBlockData.IpAddr,strtoint(_RTranFileBlockData.Prot),_PTranFileBlockData,sizeof(_PTranFileBlockData));
     end;
end;代码有点乱,希望你们能耐心的看,帮我解答一下。
就是读出来的文件数据 没有问题,可是发过去后,得到的数据 就变了。
谢谢