RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TBaseSocket.OnSend Event

Occurs just before a client socket writes information to the socket connection.

Pascal
property OnSend: TSocketDataEvent;
C++
__property TSocketDataEvent OnSend;

Write an OnSend event handler to perform actions just prior to sending data. Use OnSend to monitor the data stream and change it (for example, by removing linefeeds). If the socket is a blocking socket, use a TWinSocketStream object to write to the connection. Otherwise, use the methods of the Socket parameter to perform the actual writing.  

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!