Sample code for 30+ languages & platforms
SQL Server

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 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 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.
    DECLARE @handlerSock int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @handlerSock OUT

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

    PRINT 'Connection moved to the handler socket.'

    EXEC @hr = sp_OADestroy @listenSock
    EXEC @hr = sp_OADestroy @connectedSock
    EXEC @hr = sp_OADestroy @handlerSock


END
GO