Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Example: Socket.DupSocket method

See more Socket/SSL/TLS Examples

Demonstrates how to call the DupSocket method.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Socket;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  socket: TSocket;
  socket_for_sending: TSocket;
  socket_for_monitor: TSocket;

begin
  success := False;

  socket := TSocket.Create;
  success := socket.Connect('example.com',443,True,5000);

  //  ...
  //  ...

  //  Duplicate the socket to share the same connection across threads.
  //  In a multithreaded application, each thread should use its own socket object. For example:

  //  - The main thread can use the original socket for reading.
  //  - A secondary thread can use a duplicate for writing.
  //  - Another thread can use a separate duplicate to monitor the connection status.

  socket_for_sending := TSocket.Create;
  success := socket.DupSocket(socket_for_sending);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  socket_for_monitor := TSocket.Create;
  success := socket.DupSocket(socket_for_monitor);
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;

  //  All three socket objects: socket, socket_for_sending, and socket_for_monitor share the same underlying connection.


  socket.Free;
  socket_for_sending.Free;
  socket_for_monitor.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.