Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @listenSock int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @listenSock OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @listenSock, 'BindAndListen', @success OUT, 5000, 25
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @listenSock, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @listenSock
        RETURN
      END

    DECLARE @connectedSock int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connectedSock OUT

    EXEC sp_OAMethod @listenSock, 'AcceptNext', @success OUT, 20000, @connectedSock
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @listenSock, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @listenSock
        EXEC @hr = sp_OADestroy @connectedSock
        RETURN
      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.
    DECLARE @socketSet int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @socketSet OUT

    EXEC sp_OAMethod @socketSet, 'TakeSocket', @success OUT, @connectedSock
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socketSet, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @listenSock
        EXEC @hr = sp_OADestroy @connectedSock
        EXEC @hr = sp_OADestroy @socketSet
        RETURN
      END

    PRINT 'Connection added to the socket set.'

    EXEC @hr = sp_OADestroy @listenSock
    EXEC @hr = sp_OADestroy @connectedSock
    EXEC @hr = sp_OADestroy @socketSet


END
GO