idtcpclient idtcpserver 传送文件例子之二 writebuffer

tidpserver 端

procedure serverrecive(AThread: TIdPeerThread);
var
str: string;
fstream: tfilestream;
buff: array[0..1023] of char;
readf, filesize, readcount: integer;
begin
readf := athread.Connection.ReadInteger(); // 传送内容,1,string,2,file
str := athread.Connection.ReadLn(); //filename
str := athread.Connection.ReadLn(); //filename
filesize := athread.Connection.ReadInteger(); //filesize
progressbar1.Max := filesize;
case readf of
    1: memo1.Lines.Add(str);
    2:
      begin
        if savedialog1.Execute then
          fstream := tfilestream.Create(savedialog1.FileName, fmcreate);
        while filesize > fstream.Size do
        begin
          if FileSize - fStream.Size > SizeOf(Buff) then
            ReadCount := SizeOf(Buff)
          else
            ReadCount := FileSize - fStream.Size;
          athread.Connection.ReadBuffer(buff, readcount);
          fstream.WriteBuffer(buff, readcount);
          ProgressBar1.Position := fStream.Size;
          self.Caption := inttostr(fstream.Size) + '/' + inttostr(filesize);
          application.ProcessMessages;
        end;
        fstream.Free;
      end;
end;
end;

-------调用---------

procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);

begin

serverreceive(athread);

end;

---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------

tidpclient端

procedure tform1.idtcpclientsend;

var
fstream: tfilestream;
readcount, filesize: integer;
buff: array[0..1023] of char;

begin
if idtcpclient1.Connected then
    idtcpclient1.Disconnect;
idtcpclient1.Host := edit1.Text;
idtcpclient1.Port := strtoint(edit2.Text);
idtcpclient1.Connect();
if checkbox1.Checked then
begin
    idtcpclient1.WriteInteger(1);
    idtcpclient1.WriteLn(edit3.Text);
    idtcpclient1.WriteInteger(0);
end
else
begin
    if opendialog1.Execute then
    begin
      if extractfilename(opendialog1.FileName) = '' then
        idtcpclient1.Disconnect
      else
      begin
        fstream := tfilestream.Create(opendialog1.FileName, fmopenread);
        idtcpclient1.WriteInteger(2);
        idtcpclient1.WriteLn(opendialog1.FileName);
        filesize := fstream.Size;
        idtcpclient1.WriteInteger(filesize);
        while fstream.Position < filesize do
        begin
          if filesize - fstream.Position >= sizeof(buff) then
            readcount := sizeof(buff)
          else
            readcount := filesize - fstream.Position;
          fstream.ReadBuffer(buff, readcount);
          idtcpclient1.WriteBuffer(buff, readcount);
        end;
        fstream.Free;
        idtcpclient1.Disconnect;
      end;
    end;
end;
end;

-------调用---------

procedure tform1.......

begin

idtcpclientsend;

end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值