Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
TCP Socket Connect with Heartbeat Event Callbacks and Abort Capability
Visual Basic socket example showing how to make a TCP/IP socket connection. This example demonstrates how to use heartbeat event callbacks while the connection establishment is in progress. The connection may be aborted while in progress. Dim WithEvents socket As ChilkatSocket Private bAbortConnect As Boolean ' Making a TCP/IP socket connection to a remote hostname:port ' is easy. The Chilkat Socket ActiveX component provides ' these features when establishing a connection: ' 1) Set an overall timeout in milliseconds. ' 2) Set a hearbeat event callback timeout in milliseconds. ' 3) Determine if the connect failed because of a DNS problem. ' 4) Provides the ability to abort a connect prior to completion. Private Sub ConnectButton_Click() bAbortConnect = False ProgressBar1.Value = 0 ' Create a Chilkat Socket object and unlock. Set socket = New ChilkatSocket success = socket.UnlockComponent("Anything for 30-day trial") If (success = 0) Then MsgBox "Failed to unlock component" Exit Sub End If Dim hostname As String Dim portNum As Integer ' You may wish to test with both valid and invalid hostnames... 'hostname = "www.hdgyueuiwefisjd.com" hostname = "www.chilkatsoft.com" ' You may wish to try it with valid and invalid port numbers. portNum = 80 'portNum = 8900 ' Most likely, nothing is listening at this port, or it could be blocked by a firewall... ' Uncomment this line to simulate a long connect time to allow you ' to test your application's behavior while a connection is in progress. ' (units are in milliseconds) 'socket.DebugConnectDelay = 3000 ' Set our hearbeat event interval time in milliseconds: socket.HeartbeatMs = 1000 ' The total connect timeout is passed as an argument totalTimeoutMs = 4000 ' Connect to a remote host:port success = socket.Connect(hostname, portNum, totalTimeoutMs) If (success = 0) Then ' Did the connection fail because of a failed DNS lookup ' on the hostname? If (socket.DnsFailed = 1) Then MsgBox "Connect failed because hostname DNS lookup failed" Else MsgBox socket.LastErrorText End If Else MsgBox "Connected!" End If End Sub ' This called when an "Abort" button in the user interface ' is pressed. Private Sub AbortButton_Click() bAbortConnect = True End Sub ' The heartbeat event is called periodically while the Connect method is in progress. Private Sub socket_Heartbeat(ByVal objectID As Long, abort As Long) ' Process application user-interface events. DoEvents If bAbortConnect Then ' Setting abort = 1 causes the connect to abort. abort = 1 Else ' Setting abort = 0 causes the connect to continue waiting. abort = 0 newVal = ProgressBar1.Value + 2 If newVal <= ProgressBar1.Max Then ProgressBar1.Value = newVal End If End If End Sub
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.