Lazarus Pascal
Lazarus Pascal
Move a Connection into a Socket Set
See more Socket/SSL/TLS Examples
Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.
Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.
Chilkat Lazarus Pascal Downloads
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;
socketSet: 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 connection out of connectedSock and into a newly created member of this object's socket
// set. After one or more such calls, socketSet acts as a set of connected sockets (useful with the
// SelectForReading / SelectForWriting methods). After success, connectedSock holds no connection.
socketSet := TSocket.Create;
success := socketSet.TakeSocket(connectedSock);
if (success = False) then
begin
WriteLn(socketSet.LastErrorText);
Exit;
end;
WriteLn('Connection added to the socket set.');
listenSock.Free;
connectedSock.Free;
socketSet.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.