RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TTcpServer.Active Property

Indicates whether the socket connection is open and available for communication with other machines.

Pascal
property Active: Boolean;
C++
__property Boolean Active;

Before attempting to use or change the socket connection, read Active to determine whether the connection is open and ready. For client sockets, setting Active opens or shuts down a socket connection to another machine. For server sockets, setting Active opens or shuts down a listening connection that makes the socket available for client requests. 

At design time, set Active to true to make the socket open a connection when the application starts running (the default value is false). At runtime, use the Open or Close method to open or close the connection.  

C++ Examples: 

 

/*
    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);
}

 

Delphi Examples: 

{
    This example demontrates the use of SendStream method. It is to be
    used with the example from TTcpServer section.
}
const DEFAULT_PORT = 2501;

procedure TForm3.Button1Click(Sender: TObject);
var
  myStream : TMemoryStream;
  I : Cardinal;
  userValue : Integer;
begin
  //creating a stream
  myStream := TMemoryStream.Create();
  for I := 0 to 8 do
  begin
    myStream.WriteBuffer(I,1);
  end;
  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);
  TcpClient1.Active := false;

  //free the stream
  myStream.Free;

end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  Edit1.Text := '0';
  Memo1.Lines.Clear;

  TcpClient1.RemotePort := IntToStr(DEFAULT_PORT);
  TcpClient1.RemoteHost := 'localhost';
  TcpClient1.Active := true;
end;

procedure TForm3.TcpClient1Send(Sender: TObject; Buf: PWideChar;
  var DataLen: Integer);
begin
  Memo1.Lines.Add(DateTimeToStr(now) + ' Sent data with ' + Edit1.Text);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!