网上冲浪 | 反毒杀毒 | 菜鸟进阶 | 网络安全 | 实用技术 | 网络安全 | 操作系统 |
工具软件 | 电脑医院 | 网上赚钱 | 网页制作 | 网络营销 | 经典教程 | IT趣 谈 |
当前位置:IT快活林快活林学院操作系统

请帮忙look一下我的程序那里出错保存文件为乱码

请帮忙看一下我的程序(Procedures)(Procedures)那里出错啦?保存文件为乱码=============
定义数据(Data)包:
unit unit3;

interface
type
    p_head=(filelist,server_filelist,filelist_over);
    cmd_page=packed record
    cmd:p_head;
    end;
    file_page=packed record
    head:p_head;
    size:integer;
    body:array[0..2000] of byte;
    end;
implementation

end.
=============
控制端:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdSocketHandle,
  IdUDPServer;

type
    TForm1 =class(TForm)
    IdUDPServer1: TIdUDPServer;
    Memo1: TMemo;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
      ABinding: TIdSocketHandle);
    procedure FormCreate(Sender: TObject);
  private
    {Private declarations}
  public
    {Public declarations}
    ip:string;
  end;


var
  Form1: TForm1;
  liststream:Tmemorystream;
implementation

uses Unit2,unit3;


{$R *.dfm}

procedure TFormI.N1Click(Sender: TObject);
begin
formII.Show;
end;

procedure TFormI.N2Click(Sender: TObject);
var  cmd:cmd_page;
begin
cmd.cmd:=filelist;
idudpserverI.SendBuffer(ip,8002,cmd,sizeof(cmd));
end;

procedure TFormI.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
var buff:array[0..2050] of byte;
    head:p_head;
    list:tstringlist;
    i:integer;
    filelist:^file_page;
    stream:tstream;
    str:string;
begin
  adata.Read(buff,sizeof(adata.Size));
  labelI.Caption:=inttostr(adata.Size);
  filelist:=@buff[0];
  case filelist.head of
  server_filelist: //文件列表
  begin
  liststream.Seek(0,2);
  liststream.WriteBuffer(filelist.body,filelist.size);
  end;
  filelist_over://
  begin

  liststream.Position:=0;
  list:=tstringlist.Create;
  list.LoadFromStream(liststream);
  liststream.SaveToFile('I.txt');

  {for i:=0 to list.Count-1 do
  begin
  memoI.Lines.Add(list.Strings[i]);
  end; }


  list.Free;
  liststream.Clear;
  end;
  end;
end;

procedure TFormI.FormCreate(Sender: TObject);
begin
  liststream:=tmemorystream.Create;
end;

end.
=========================
服务端:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, StdCtrls,IdSocketHandle;

type
  TForm1 =class(TForm)
    IdUDPServer1: TIdUDPServer;
    Label1: TLabel;
    procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
      ABinding: TIdSocketHandle);
  private
    {Private declarations}
  public
    {Public declarations}
  end;

var
  Form1: TForm1;
  ip:string;
  i:integer;
implementation
uses unit3;
{$R *.dfm}

procedure findfile(path:string;list:tstringlist);
var h:thandle;
    att:TWIN32FindData;
    filename:string;
begin
if path[length(path)] <>'\' then path:=path+'\';
path:=path+'*.*';
h:=Microsoft Windows.FindFirstFile(pchar(path),att);
while h <>INVALID_HANDLE_VALUE do
begin
    filename:=att.cFileName;
    if (filename <>'.') and (filename <>'..') and (att.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY) then
    begin
        findfile(sysutils.ExtractFilePath(path)+filename,list);  //目录
    end
    else  if (filename <>'.') and (filename <>'..') and (att.dwFileAttributes <>FILE_ATTRIBUTE_DIRECTORY) then
    begin
        //文件
        list.Add(sysutils.ExtractFilePath(path)+filename) ;
    end;
    if not Microsoft Windows.FindNextFile(h,att) then h:=INVALID_HANDLE_VALUE;//该目录下文件及目录已经遍历完毕;
end;
end;

procedure TFormI.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
  var buff:array[0..2050] of byte;
      cmd:p_head;
      list:tstringlist;
      liststream:Tmemorystream;
      posi,sendsize:integer;
      file_trans:file_page;
begin
  ip:=abinding.PeerIP;
  adata.Read(buff,sizeof(adata.Size));

  move(buff,cmd,sizeof(cmd));//读取包头;
  case cmd of
  filelist: //文件列表
  begin
    list:=tstringlist.Create;
    liststream:=Tmemorystream.Create;
    findfile('D:\',list);
    list.SaveToStream(liststream);
    liststream.SaveToFile('I.txt');
    posi:=0;
    while posi <liststream.Size do
    begin
      sendsize:=2000;
      if sendsize>liststream.Size then sendsize:=liststream.Size;
      file_trans.head:=server_filelist;
      liststream.Read(file_trans.body,sendsize);
      file_trans.size:=sendsize;
      idudpserverI.SendBuffer(IP,8001,file_trans,sizeof(file_trans));

      posi:=posi+sendsize;
    end;

    labelI.Caption:=inttostr(sizeof(file_trans));
    file_trans.head:=filelist_over;
    idudpserverI.SendBuffer(IP,8001,file_trans,sizeof(file_trans));
    list.Free;
  end;
  end;
end;

end.
为什么或者说怎么会保存的文件是乱码呢?程序(Procedures)(Procedures)如何改,请指点?--
发送的时候对 list.Count 进行循环,每次发一行.

接收的时候同理,每收到一行就添加到 list 里.


而不能是不是把所有数据(Data)接到一起再处理.
[]作者:本站整理  来源:不祥