Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
listenSock: HCkSocket;
connectedSock: HCkSocket;
socketSet: HCkSocket;

begin
success := False;

listenSock := CkSocket_Create();

success := CkSocket_BindAndListen(listenSock,5000,25);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(listenSock));
    Exit;
  end;

connectedSock := CkSocket_Create();
success := CkSocket_AcceptNext(listenSock,20000,connectedSock);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(listenSock));
    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 := CkSocket_Create();
success := CkSocket_TakeSocket(socketSet,connectedSock);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socketSet));
    Exit;
  end;
Memo1.Lines.Add('Connection added to the socket set.');

CkSocket_Dispose(listenSock);
CkSocket_Dispose(connectedSock);
CkSocket_Dispose(socketSet);