//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "TTcpClientSendStream.h"
#include <vcl.h>
#include "time.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------

// BeginExample: TTcpClientSendStream
// Class: Sockets.TTcpClient
// Method: Sockets.TBaseSocket.SendStream
// Method: Sockets.TBaseSocket.OnSend
// Property: Sockets.TIpSocket.RemoteHost
// Property: Sockets.TIpSocket.RemotePort
// Property: Sockets.TBaseSocket.Active

// BeginCode
/*
	This example demontrates the use of SendStream method. It is to be
	used with the example from TTcpServer section.
*/
#define DEFAULT_PORT 2501

__fastcall TForm3::TForm3(TComponent* Owner)
	: TForm(Owner)
{
	Edit1->Text = "0";
	Memo1->Lines->Clear();

	TcpClient1 = new TTcpClient(this);
	TcpClient1->OnSend = TcpClient1Send;
	TcpClient1->RemotePort = DEFAULT_PORT;
	TcpClient1->RemoteHost = "localhost";
}

#include <memory>       //for STL auto_ptr class

void __fastcall TForm3::Button1Click(TObject *Sender)
{
	//creating a stream
//	TMemoryStream* myStream = new TMemoryStream();
	std::auto_ptr<TMemoryStream> myStream(new TMemoryStream);
	for (byte i = 0; i < 9; i++)
	{
		myStream->WriteBuffer(&i,1);
	}
	int userValue = StrToIntDef(Edit1->Text, 0);
	myStream->WriteBuffer(&userValue, 1);

	//resetting the stream position
	myStream->Seek(0, 0);

	//sending the stream
	TcpClient1->Active = true;
	TcpClient1->SendStream(myStream.get());
	TcpClient1->Active = false;

	//free the stream
//	delete myStream;
}

//---------------------------------------------------------------------------

void __fastcall TForm3::TcpClient1Send(TObject *Sender, PWideChar Buf, int &DataLen)

{
	TDateTime now = TDateTime::CurrentDateTime();
	Memo1->Lines->Add(DateTimeToStr(now) + " Sent data with " + Edit1->Text);
}
// EndCode
// EndExample: TTcpClientSendStream
//---------------------------------------------------------------------------

