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

Move a Connection into Another Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeConnection, which moves the active connection from one Socket into another. The receiving object first releases any connection it previously held.

Background. Unlike TakeSocket, TakeConnection does not add a member to a socket set. After success, the source socket holds no connection. This is useful for handing an accepted connection to a dedicated handler object.

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;
  listenSock: TSocket;
  connectedSock: TSocket;
  handlerSock: TSocket;

begin
  success := False;

  listenSock := TSocket.Create;

  success := listenSock.BindAndListen(5000,25);
  if (success = False) then
    begin
      WriteLn(listenSock.LastErrorText);
      Exit;
    end;

  connectedSock := TSocket.Create;
  success := listenSock.AcceptNext(20000,connectedSock);
  if (success = False) then
    begin
      WriteLn(listenSock.LastErrorText);
      Exit;
    end;

  //  Move the active connection out of connectedSock and into another Socket object.  Unlike
  //  TakeSocket, this does not add a member to a socket set.  After success, connectedSock holds no
  //  connection.
  handlerSock := TSocket.Create;
  success := handlerSock.TakeConnection(connectedSock);
  if (success = False) then
    begin
      WriteLn(handlerSock.LastErrorText);
      Exit;
    end;
  WriteLn('Connection moved to the handler socket.');


  listenSock.Free;
  connectedSock.Free;
  handlerSock.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.