Silent Socket Receiver in Delphi

The tutorial represents a Delphi program that starts listening to an incoming connection, accepts the connection, receives all data over the established connection, and sends no data back to the client.
 
 
This Delphi program represents a mostly simple demos and by no means, a complete application. It's intended to demonstrate how to use the TcpServerConnection socket class from the Clever Internet Suite library to start listening on the specified local port and receive TCP packets over the Network.
 
This functionality might be useful when you debug a Network app and want to analyze the produced TCP traffic. You can run the Silent Socket Receiver app and capture the Network packets between the client and the server, e.g. using the Wireshark utility. In this case your Network app acts as the client, and the Silent Socket Receiver app as the server.
 
You can even modify the Silent Socket Receiver program to send TCP response back to the connected client.
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  StartupSocket();
  FConnection := TclTcpServerConnection.Create();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FConnection.Free();
  CleanupSocket();
end;
procedure TForm1.btnListenClick(Sender: TObject);
var
  data: TStream;
begin
  try
    FConnection.NetworkStream := TclNetworkStream.Create();
    FConnection.Listen(StrToInt(edtPort.Text));

    memLog.Lines.Add('=== Start listening ===');
    FStarted := True;

    FConnection.TimeOut := -1;
    FConnection.Accept();

    data := TMemoryStream.Create();
    try
      repeat
        FConnection.ReadData(data);
        memLog.Lines.Add('Received bytes: ' + IntToStr(data.Size));
        Application.ProcessMessages();
        data.Size := 0;
      until not FConnection.Active;
    finally
      data.Free();
    end;

  except
    on E: EclSocketError do
    begin
      if ((E.ErrorCode <> 10053) and (E.ErrorCode <> 10038)) then raise;
    end;
  end;
end;
 
See also:
 
How to compile:
  1. Please clone the GitHub/CleverComponents/Clever-Internet-Suite-Tutorials repository.
  2. Download and install the Clever Internet Suite library.
  3. Open and compile the SilentSocketReceiver project in your Delphi IDE.
  4. Enjoy.
 
Have questions?
 
Please feel free to Contact Us
Join us on Facebook   YouTube   Twitter   Telegram   Newsletter
 
Kind regards
Clever Components team
www.CleverComponents.com

Add Feedback