RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomTcpServer.OnAccept Event

Occurs on server sockets just after the connection to a client socket is accepted.

Pascal
property OnAccept: TSocketAcceptEvent;
C++
__property TSocketAcceptEvent OnAccept;

Write an OnAccept event handler for a server socket to take specific action after the connection to a client socket has been accepted. This is the first point when information is available about the local port and IP address that is used in the server endpoint of the connection. The port number, and possibly the IP address, differs from those of the listening socket.  

Server sockets open a socket connection for listening to establish a queue to hold client requests. After a request from the queue is accepted by the server socket, an OnAccept event occurs. After the server socket accepts the connection, the client socket completes the connection, and an OnConnect event occurs for the client socket.  

C++ Examples: 

 

/*
This example demonstrated the use of TTcpServer in order to receive
a buffer (stream) and process it. It is to be used with the example
from TTcpClient section.
This example uses three buttons, one for activating the server, one
for deactivating it and the third for clearing the Memo.
*/
__fastcall TForm3::TForm3(TComponent* Owner)
    : TForm(Owner)
{
    Memo1->Lines->Clear();

    //server initialization
    TcpServer1 = new TTcpServer(this);
    TcpServer1->OnAccept = TcpServer1Accept;
    TcpServer1->OnCreateHandle = TcpServer1CreateHandle;
    TcpServer1->OnDestroyHandle = TcpServer1DestroyHandle;

}
//---------------------------------------------------------------------------
void __fastcall TForm3::TcpServer1Accept(TObject *Sender,
                                        TCustomIpClient *ClientSocket)
{
    byte a[10];
    ClientSocket->ReceiveBuf(a,10,0);
    TDateTime now = TDateTime::CurrentDateTime();
    Memo1->Lines->Add(DateTimeToStr(now) + " Data added width " +
                        IntToStr(a[9]));
    for (int i=0; i < 9; i++)
    {
        Memo1->Lines->Add(IntToStr(a[i] + a[9]));
    }
    Memo1->Lines->Add("--------------------");

}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button1Click(TObject *Sender)
{
    TcpServer1->LocalPort = DEFAULT_PORT;
    TcpServer1->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm3::Button2Click(TObject *Sender)
{
    TcpServer1->Active = false;
}

//---------------------------------------------------------------------------
void __fastcall TForm3::Button3Click(TObject *Sender)
{
    Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm3::TcpServer1CreateHandle(TObject *Sender)
{
    TDateTime now = TDateTime::CurrentDateTime();
    Memo1->Lines->Add(DateTimeToStr(now) + " Server started.");
}
//---------------------------------------------------------------------------

void __fastcall TForm3::TcpServer1DestroyHandle(TObject *Sender)
{
    TDateTime now = TDateTime::CurrentDateTime();
    if(this->Memo1)
    {
        Memo1->Lines->Add(DateTimeToStr(now) + " Server stopped.");
    }
}
//---------------------------------------------------------------------------

 

Delphi Examples: 

{
This example demonstrated the use of TTcpServer in order to receive
a buffer (stream) and process it. It is to be used with the example
from TTcpClient section.
}

const DEFAULT_PORT = 2501;

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
  //connectiong the server
  TcpServer1.Active := true;
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
  //disconnecting the server
  TcpServer1.Active := false;
end;

procedure TForm3.Button3Click(Sender: TObject);
begin
  //clearing the memo
  Memo1.Lines.Clear;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Clear;

  //server initialization
  TcpServer1 := TTcpServer.Create(Self);
  TcpServer1.OnAccept := TCpServer1Accept;
  TcpServer1.OnCreateHandle := TcpServer1CreateHandle;
  TcpServer1.OnDestroyHandle := TcpServer1DestroyHandle;
  TcpServer1.LocalPort := IntToStr(DEFAULT_PORT);
end;

procedure TForm3.TcpServer1Accept(Sender: TObject;
  ClientSocket: TCustomIpClient);
var
  a : array[0..9] of byte;
  I : Cardinal;
begin

  ClientSocket.ReceiveBuf(a,10,0);
  Memo1.Lines.Add(DateTimeToStr(now) + ' Data added with ' +
                  IntToStr(a[9]));
  for I := 0 to 8 do
  begin
    Memo1.Lines.Add(IntToStr(a[I] + a[9]));
  end;
  Memo1.Lines.Add('--------------------');

end;

procedure TForm3.TcpServer1CreateHandle(Sender: TObject);
begin
  Memo1.Lines.Add(DateTimeToStr(now) + ' Server started.');
end;

procedure TForm3.TcpServer1DestroyHandle(Sender: TObject);
begin
  if Self.Memo1 <> nil then
    Memo1.Lines.Add(DateTimeToStr(now) + ' Server stopped.');
end;

 

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