我使用indy控件的tcpclient和tcpserver来向网络中别的机器发送文件,对于小文件速度比较快,(不大于5M)但是碰到大文件就有问题了,程序总是无响映。不知道是怎么回事,下面是发送和接收的代码,请大虾指教。。发送
procedure TForm1.Button1Click(Sender: TObject);
var
buf: TMemoryStream;//创建内存对像
begin
buf :=TMemoryStream.Create; //创建
  buf.LoadFromFile(edit2.Text); //得到文件路径
  clinesno:=listbox2.itemindex;  //得到当前行号
  ipaddress:=listbox2.Items.Strings[clinesno];//得到ip地址
  edit1.Text:=(GetIPFromName(ipaddress));// 可得到ip地址
idtcpclient1.Connect();
        idtcpclient1.WriteLn(edit2.text); //发送文件路径
        IdTcpClient1.WriteStream(buf); //发送文件
        sleep(10);
        IdTcpClient1.Disconnect;
        buf.Free;
end;接收方程序
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  buf: TMemoryStream;
  filenames:string;
  begin
filenames:=athread.Connection.ReadLn;
 buf := TMemoryStream.Create;
 AThread.Connection.ReadStream(buf, -1, true);
 buf.SaveToFile(filenames);
buf.Free;
end;
还有一个问题,使用tmemorystream是不是将文件先读入内存,然后再发送给对方,对方也是同样的用tmemorystream读入内存,然后存盘?
如果这样的话,想传送一个300-400M的文件,而内存又没有那么大,这时会不会出现错误?还是windows会自己调用虚似内存?另外上面的程序的效率怎么样?能否再快一点,请大家指教,谢谢

解决方案 »

  1.   

    正如你所说,内存不足的问题,直接用 TFileStream 应该可以
      

  2.   

    那应该怎么改?
    我把tmemorystream
    改成tfilestream
    不能编辑
      

  3.   

    发送:IdTcpClient1.WriteStream(buf, True, True); 
    接收:AThread.Connection.ReadStream(buf, -1, False);
    应该是没有问题的。
    这样它就会把流的长度发过去,接收时就知道接收多少字节。否则就会一直接收到对方关闭连接。
    考虑到内存问题,最好使用TFileStream.
      

  4.   

    伤心大哥
    可是我将tmemorystream改成tfilestream时,
    在buf:=tfilestream.create;
    时出错,不知道为什么。
      

  5.   

    buf:=TFilestream.Create(这里怎么写?)
      

  6.   

    发送时:
    buf := TFileStream.Create(FileName, fmOpenRead);
    接收时:
    buf := TFileStream.Create(FileName, fmCreate);
      

  7.   

    不行呀!
    还是错
    我定义了
    filename:file;
    也不行!!!
    出错!
    There is no overloaded version of 'Create' that can be called with these arguments**************************************************************************符原码procedure TForm1.ListBox2Click(Sender: TObject);
    begin
    clinesno:=listbox2.itemindex;  //得到当前行号
    ipaddress:=listbox2.Items.Strings[clinesno];//得到ip地址
    edit1.Text:=(GetIPFromName(ipaddress));// 可得到ip地址
    listboxh:=listbox2.SelCount;
    end;procedure Tform1.AddLogMsg(const aMsg: string);
    begin
      Log.Lines.Add(aMsg);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
    filenames:file;
    buf:tfilestream;
    //buf: TMemoryStream;//创建内存对像
    xh:integer;
    begin
    if edit2.text='' then
    begin
    label1.Caption:='请注意,没有选择文件,不能完成操作!';
    end
    else
    begin
    end;
    if edit1.text='' then
    begin
    label1.Caption:='请注意,没有选择计算机,不能完成操作!';
    end
    else
    begin
    end;
    if check>=2 then
    begin
      buf := TFileStream.Create(FileNames, fmOpenRead);
    //buf :=TMemoryStream.Create; //创建
      buf.LoadFromFile(edit2.Text); //得到文件路径
      clinesno:=listbox2.itemindex;  //得到当前行号
      ipaddress:=listbox2.Items.Strings[clinesno];//得到ip地址
      edit1.Text:=(GetIPFromName(ipaddress));// 可得到ip地址
        if listboxh>1 then
        begin
          for xh:=0 to listboxh-1 do
          begin
            ipaddress:=listbox2.Items.Strings[xh];
            edit1.Text:=(getipfromname(ipaddress));
            idtcpclient1.Host:=ipaddress;
            idtcpclient1.Connect();
            idtcpclient1.WriteLn(edit2.text); //发送文件路径
            AddLogMsg(Format('文件读取 %s, 文件大小=%d',[edit2.Text, buf.Size]));
             //idtcpclient1.WriteFile(edit2.Text,true);
            IdTcpClient1.WriteStream(buf,true,true); //发送文件
            sleep(10);
            IdTcpClient1.Disconnect;
            buf.Free;        end;
        end
        else
            begin
              idtcpclient1.Host:=ipaddress; //得到ip地址
              idtcpclient1.Connect();    //连接
              idtcpclient1.WriteLn(edit2.text); //发送文件路径
              AddLogMsg(Format('文件读取 %s, 文件大小=%d',[edit2.Text, buf.Size]));
              //idtcpclient1.WriteFile(edit2.Text,true);
              IdTcpClient1.WriteStream(buf,true,true); //发送文件
              sleep(10);
              IdTcpClient1.Disconnect;
              buf.Free;
        end;
        end
    else
      begin
        exit;
    end;
    end;
      

  8.   

    FileName是指文件名。
      buf := TFileStream.Create(FileNames, fmOpenRead);
    //buf :=TMemoryStream.Create; //创建
      buf.LoadFromFile(edit2.Text); //得到文件路径
    改为
      buf := TFileStreamCreate(edit2.Text, fmOpenRead);
      

  9.   

    问题解决了,谢谢你,另外还想问一下
    c:\ssss\adfa\asdfasdf\你好\aaaa.exe
    我怎么取出aaaa.exe这些字符?