SQL Server
SQL Server
Receive a 16-bit Integer from a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveInt16, which reads exactly two bytes as a 16-bit integer and stores the result in the ReceivedInt property. Byte order and signedness are selectable.
Background. Fixed-size integers and length prefixes are common building blocks for application-defined binary protocols. Byte order is selectable, with big-endian being network byte order.
Chilkat SQL Server Downloads
--
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
-- Read exactly two bytes as a 16-bit integer and store the result in the ReceivedInt property.
-- The 1st argument selects byte order (1 = big-endian); the 2nd selects unsigned interpretation.
DECLARE @bBigEndian int
SELECT @bBigEndian = 1
DECLARE @bUnsigned int
SELECT @bUnsigned = 0
EXEC sp_OAMethod @socket, 'ReceiveInt16', @success OUT, @bBigEndian, @bUnsigned
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
EXEC sp_OAGetProperty @socket, 'ReceivedInt', @iTmp0 OUT
PRINT 'Received 16-bit integer: ' + @iTmp0
EXEC @hr = sp_OADestroy @socket
END
GO