RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TTcpClient.RemotePort Property

Specifies the ID number of the port used by the RemoteHost to form the socket connection.

Pascal
property RemotePort: TSocketPort;
C++
__property TSocketPort RemotePort;

Use RemotePort to determine the port number used by the socket on the other end of the socket connection.  

Port numbers allow a single system, identified by the RemoteHost property, to host multiple connections simultaneously. Each combination of RemotePort and RemoteHost can only be bound to a single socket connection.  

Many port numbers are associated by convention with a particular service such as ftp or http. To determine whether the value of RemotePort is associated with a specific service, use the LookupProtocol method.

Note: If a client socket requests a specific port to indicate a desired service, the value of RemotePort for the actual connection may differ from the requested port number. Most server sockets listen on the port associated with a service, but switch to an arbitrary available port number for forming connections.
 

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!