Sample code for 30+ languages & platforms
SQL Server

Example: Socket.AcceptNext method

Demonstrates how to call the AcceptNext method.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

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

    DECLARE @port int
    SELECT @port = 5555
    DECLARE @backlog int
    SELECT @backlog = 25
    EXEC sp_OAMethod @listenSocket, 'BindAndListen', @success OUT, @port, @backlog
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @listenSocket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @listenSocket
        EXEC @hr = sp_OADestroy @connectedSocket
        RETURN
      END

    -- Accept next incoming connection
    DECLARE @maxWaitMs int
    SELECT @maxWaitMs = 200000
    EXEC sp_OAMethod @listenSocket, 'AcceptNext', @success OUT, @maxWaitMs, @connectedSocket
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @listenSocket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @listenSocket
        EXEC @hr = sp_OADestroy @connectedSocket
        RETURN
      END

    -- ...
    -- ...

    SELECT @maxWaitMs = 20000
    EXEC sp_OAMethod @connectedSocket, 'Close', @success OUT

    EXEC @hr = sp_OADestroy @listenSocket
    EXEC @hr = sp_OADestroy @connectedSocket


END
GO