Sample code for 30+ languages & platforms
SQL Server

Accept the Next Incoming Connection

See more Socket/SSL/TLS Examples

Demonstrates Socket.AcceptNext, which waits for and accepts the next incoming connection on a listening socket, delivering it as a new connected Socket.

Background. A maxWaitMs of 0 waits indefinitely; a positive value waits up to that many milliseconds. For a TLS listener, acceptance includes the server-side TLS handshake.

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 @socket int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    --  Wait for and accept the next incoming connection on the listening socket.  maxWaitMs = 0 waits
    --  indefinitely; a positive value waits up to that many milliseconds.  For a TLS listener, acceptance
    --  includes the server-side TLS handshake.
    DECLARE @connectedSock int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connectedSock OUT

    EXEC sp_OAMethod @socket, 'AcceptNext', @success OUT, 20000, @connectedSock
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        EXEC @hr = sp_OADestroy @connectedSock
        RETURN
      END

    EXEC sp_OAGetProperty @connectedSock, 'RemoteIpAddress', @sTmp0 OUT
    PRINT 'Accepted a connection from ' + @sTmp0

    EXEC @hr = sp_OADestroy @socket
    EXEC @hr = sp_OADestroy @connectedSock


END
GO