SQL Server
SQL Server
Check Internet Connectivity
See more Socket/SSL/TLS Examples
Demonstrates an efficient way to test for Internet connectivity.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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
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
-- 1. Use a reliable Anycast IP.
-- 8.8.8.8 (Google) or 1.1.1.1 (Cloudflare) are the industry standards.
DECLARE @targetIp nvarchar(4000)
SELECT @targetIp = '8.8.8.8'
-- 2. Use Port 53 (DNS).
-- DNS servers typically listen on TCP Port 53 as well as UDP.
-- (Alternatively, use port 443 if you suspect port 53 is blocked).
DECLARE @port int
SELECT @port = 53
-- 3. Disable SSL (0).
-- We are not doing a handshake, just a TCP connection.
DECLARE @ssl int
SELECT @ssl = 0
-- 4. Short Timeout (1500ms).
-- If you can't reach Google in 1.5 seconds, the connection is
-- likely too poor for practical use anyway.
DECLARE @timeoutMs int
SELECT @timeoutMs = 1500
-- Connect
EXEC sp_OAMethod @socket, 'Connect', @success OUT, @targetIp, @port, @ssl, @timeoutMs
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
EXEC sp_OAMethod @socket, 'Close', @success OUT, 10
PRINT 'We have Internet connectivity.'
EXEC @hr = sp_OADestroy @socket
END
GO