Sample code for 30+ languages & platforms
SQL Server

Bind and Listen for Incoming Connections

See more Socket/SSL/TLS Examples

Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.

Background. The backlog argument bounds the queue of pending connections. When ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.

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

    --  Bind a TCP listener to a port and place it into listening mode.  The 2nd argument is the backlog,
    --  the maximum number of pending connections allowed to queue.
    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

    --  After the listener is established, accept the next incoming connection into a new Socket.
    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

    PRINT 'Accepted a connection.'

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


END
GO