Sample code for 30+ languages & platforms
SQL Server

Connect a TCP or TLS Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.Connect, which establishes a TCP connection to a host and port. Setting the TLS argument to _TRUE_ performs a TLS handshake after the TCP connection succeeds.

Background. The hostname may be a DNS name or a numeric IPv4/IPv6 address. The final argument bounds how long the connect attempt waits before failing. On failure, ConnectFailReason and LastErrorText provide detail.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 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.  The 3rd argument enables the TLS handshake after the TCP
    --  connection succeeds; the 4th is the maximum time to wait, in milliseconds.
    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

    --  StringCharset controls the character encoding used when sending and receiving text.
    EXEC sp_OASetProperty @socket, 'StringCharset', 'utf-8'

    --  The socket is now connected and ready to send and receive data using whatever protocol the
    --  application implements.

    EXEC sp_OAGetProperty @socket, 'IsConnected', @iTmp0 OUT
    PRINT 'Connected: ' + @iTmp0

    --  Close the connection when finished.  The argument is the maximum time to wait for a clean
    --  shutdown, in milliseconds.
    EXEC sp_OAMethod @socket, 'Close', @success OUT, 1000
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @socket
        RETURN
      END

    EXEC @hr = sp_OADestroy @socket


END
GO