Sample code for 30+ languages & platforms
SQL Server

Wait for a Socket to Become Writable

See more Socket/SSL/TLS Examples

Demonstrates Socket.SelectForWriting, which waits for the socket, or sockets in a socket set, to become writable. It returns 1 if writable, 0 on timeout, or -1 on error.

Background. A timeoutMs of 0 performs a nonblocking poll. This is useful before attempting to send when the peer may be applying flow control.

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

    --  Connect to the server using TLS.
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @maxWaitMs int
    SELECT @maxWaitMs = 5000
    EXEC sp_OAMethod @socket, 'Connect', @success OUT, 'example.com', 5000, @bTls, @maxWaitMs
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        RETURN
      END

    --  Wait up to 5 seconds for the socket (or sockets in a socket set) to become writable.  Returns 1 if
    --  writable, 0 on timeout, or -1 on error.
    DECLARE @ready int
    EXEC sp_OAMethod @socket, 'SelectForWriting', @ready OUT, 5000
    IF @ready < 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        RETURN
      END
    IF @ready <> 1
      BEGIN

        PRINT 'The socket did not become writable before the timeout.'
        EXEC @hr = sp_OADestroy @socket
        RETURN
      END

    PRINT 'The socket is ready for writing.'

    EXEC @hr = sp_OADestroy @socket


END
GO