SQL Server
SQL Server
Receive an Exact Number of Bytes as Encoded Text
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveNBytesENC, which receives exactly the requested number of bytes and returns them encoded using a named encoding. It waits until the full count is received.
Background. Inactivity is controlled by
MaxReadIdleMs: the call fails if incoming bytes halt for longer than MaxReadIdleMs milliseconds before the full amount is received. The ENC methods move raw binary data to and from printable text using a named encoding such as base64 or hex.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
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.
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
-- MaxReadIdleMs controls the inactivity limit for receiving. The ReceiveNBytesENC call will fail if
-- incoming bytes halt for longer than this many milliseconds before the full amount is received.
EXEC sp_OASetProperty @socket, 'MaxReadIdleMs', 15000
-- Receive exactly the requested number of bytes and return them encoded using the named encoding.
-- Unlike ReceiveBytesENC, this waits until the full count has been received.
DECLARE @hex nvarchar(4000)
EXEC sp_OAMethod @socket, 'ReceiveNBytesENC', @hex OUT, 16, 'hex'
EXEC sp_OAGetProperty @socket, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
PRINT @hex
EXEC @hr = sp_OADestroy @socket
END
GO