下面是书上的一个例子,好像并没有显式地指出stream的大小,读出的时候也没有显示地指出。不知道它的内部工作的如何进行的 。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, IdTCPConnection, IdTCPClient,
IdBaseComponent, IdComponent, IdTCPServer;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
Image1: TImage;
Image2: TImage;
BitBtn1: TBitBtn;
Bevel1: TBevel;
Bevel3: TBevel;
Bevel4: TBevel;
procedure BitBtn1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
Temp:TMemoryStream;
begin
Temp:=TMemoryStream.Create;
//将图像文件保存到内存流
Image1.Picture.Graphic.SaveToStream(Temp);
//发送图像信息
IdTCPClient1.Connect();
IdTCPClient1.writestream(Temp);
IdTCPClient1.Disconnect();
Temp.Free ;
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
const
TempImageFile='c:/test.bmp';
//设置的临时图像文件
var
t:TMemoryStream;
begin
t:=TMemoryStream.Create;
AThread.Connection.ReadStream(t,-1,True);
//从连接中取得流式数据
t.SaveToFile(TempImageFile);
Image2.Picture.LoadFromFile(TempImageFile);
t.Free ;
end;
end.