2.客戶端連接到服務器後,發送一個或多個流,當斷開的時候,服務器將所有流合並在一起接收。
{*------------------------------------------------------------------------------
客戶端發送流,假定還未建立連接
------------------------------------------------------------------------------*}
procedure TClIEntForm.Button3Click(Sender: TObject);
var s: string; stream: TStream;
begin
IdTCPClIEnt1.Connect;
try
s := 'Hello world!';
stream := TStringStream.Create(s);
IdTCPClIEnt1.OpenWriteBuffer;
IdTCPClIEnt1.WriteStream(stream, true);
finally
IdTCPClIEnt1.CloseWriteBuffer;
stream.Free;
IdTCPClIEnt1.Disconnect;
end;
end;
{*------------------------------------------------------------------------------
服務器接收流
------------------------------------------------------------------------------*}
procedure TServerForm.IdTCPServer1Execute(AThread: TIdPeerThread);
var stream: Tstream;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
stream := TStringStream.Create('');
AThread.Connection.ReadStream(stream,-1,true);//當斷開連接時候接收流
stream.Position := 0;
Memo1.Lines.LoadFromStream(stream);
end;
end;