Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Accept Connection Synchronously with Heartbeat Events and Abort
Demonstrates how to listen at a port and accept the next incoming TCP socket connection. Dim WithEvents socket As ChilkatSocket Private bAbortAccept As Boolean Private Sub AbortButton_Click() bAbortAccept = True End Sub ' Demonstrates how to listen and wait for incoming TCP connections ' at a port. The AcceptNextConnection call is synchronous. Heartbeat ' events are periodically fired to allow the application to abort waiting ' for a connection before the max timeout. The Chilkat Socket ActiveX ' also provides methods for asynchronous accept. Private Sub AcceptButton_Click() 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 ' Bind the TCP socket to a port and configure it for listening. Port = 132 backlog = 25 success = socket.BindAndListen(Port, backlog) If (success = 0) Then MsgBox socket.LastErrorText Exit Sub End If ' Accept the next incoming connection. ' Wait a maximum of 10 seconds maxWaitMs = 10000 ' Make a heartbeat callback every .2 seconds socket.HeartbeatMs = 200 ' Make a synchronous call to accept the next incoming connection. Dim socket2 As ChilkatSocket Set socket2 = socket.AcceptNextConnection(maxWaitMs) If socket2 Is Nothing Then MsgBox socket.LastErrorText Exit Sub End If List1.AddItem ("connected!") End Sub ' The heartbeat event is called when a blocking call is in progress, ' such as an AcceptNextConnection... ' Each Chilkat Socket object has a unique objectID ' The object ID is passed to the heartbeat event to allow ' your application to know which socket object fired the event. Private Sub socket_Heartbeat(ByVal objectID As Long, abort As Long) ' Indicate that the AcceptNextConnection is waiting... newValue = ProgressBar1.Value + 1 If newValue <= ProgressBar1.Max Then ProgressBar1.Value = newValue End If ' Call DoEvents to allow the application to process user-interface events. DoEvents ' Did the Abort button get pressed? If bAbortAccept Then ' Setting abort = 1 causes the AcceptNextConnection to abort. abort = 1 Else ' Setting abort = 0 causes the AcceptNextConnection to continue waiting. abort = 0 End If End Sub
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.