Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Socket Receive String Until Specific Byte Value is ReceivedDemonstrates the Chilkat Socket ReceiveStringUntilByte method.
-- 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 -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @sock int -- Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Socket', @sock OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- -------------------------------------------------------------------- -- This example uses the public TCP echo service at https://tcpbin.com/ -- -------------------------------------------------------------------- DECLARE @useTls int SELECT @useTls = 0 DECLARE @port int SELECT @port = 4242 DECLARE @maxWaitMs int SELECT @maxWaitMs = 5000 DECLARE @success int EXEC sp_OAMethod @sock, 'Connect', @success OUT, 'tcpbin.com', @port, @useTls, @maxWaitMs IF @success = 0 BEGIN EXEC sp_OAGetProperty @sock, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sock RETURN END -- Wait a max of 2 seconds for a response.. EXEC sp_OASetProperty @sock, 'MaxReadIdleMs', 2000 -- Send some strings, each terminated by a 0 byte. EXEC sp_OAMethod @sock, 'SendString', @success OUT, 'This is the 1st string' EXEC sp_OAMethod @sock, 'SendByte', @success OUT, 0 EXEC sp_OAMethod @sock, 'SendString', @success OUT, 'This is string number 2' EXEC sp_OAMethod @sock, 'SendByte', @success OUT, 0 -- The tcpbin.com echo server only echoes after receiving an LF (linefeed char) EXEC sp_OAMethod @sock, 'SendByte', @success OUT, 10 -- Now let's receive what is echoed back.... -- Receive each string and the NULL byte. -- The call to ReceiveStringUntilByte will receive incoming data until the lookForByte is encountered. -- The lookForByte is receive and discarded (it is not returned in the string) DECLARE @lookForByte int SELECT @lookForByte = 0 DECLARE @s1 nvarchar(4000) EXEC sp_OAMethod @sock, 'ReceiveStringUntilByte', @s1 OUT, @lookForByte EXEC sp_OAGetProperty @sock, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @sock, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sock RETURN END PRINT @s1 DECLARE @s2 nvarchar(4000) EXEC sp_OAMethod @sock, 'ReceiveStringUntilByte', @s2 OUT, @lookForByte EXEC sp_OAGetProperty @sock, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @sock, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sock RETURN END PRINT @s2 -- The echo server will also echo back the final LF EXEC sp_OAMethod @sock, 'ReceiveByte', @success OUT, 1 SELECT @maxWaitMs = 100 EXEC sp_OAMethod @sock, 'Close', @success OUT, 100 -- Output: -- This is the 1st string -- This is string number 2 EXEC @hr = sp_OADestroy @sock END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.