Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Asynchronously Accepting Socket ConnectionsDemonstrates how to asynchronously accept a TCP socket connection using the Chilkat Socket ActiveX component. Private Sub AsyncAccept_Click()
' VB example showing how to listen on a port and accept the next
' TCP connection in a background thread.
Dim accSocket As New ChilkatSocket
Dim connectedSocket As ChilkatSocket
success = accSocket.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.
' This call does not block.
port = 132
backlog = 25
success = accSocket.BindAndListen(port, backlog)
If (success = 0) Then
MsgBox accSocket.LastErrorText
Exit Sub
End If
' Setting maxWaitMs = 0 indicates that there is no timeout.
maxWaitMs = 0
' Wait for an incoming connection on a background thread.
success = accSocket.AsyncAcceptStart(maxWaitMs)
' You may abort the background thread at any point by calling AsyncAcceptAbort.
' This example aborts after 1 second
'accSocket.SleepMs 1000
'accSocket.AsyncAcceptAbort
' Wait for the connect to complete, fail, or abort.
Do
DoEvents
accSocket.SleepMs 10
Loop Until (accSocket.AsyncAcceptFinished = 1)
' You may check the completion status by:
If (accSocket.AsyncAcceptFinished = 1) Then
MsgBox "Background TCP accept thread is finished"
' If finished, you may check the success/failure status:
If (accSocket.AsyncAcceptSuccess = 1) Then
MsgBox "TCP socket connection accepted!"
' The socket object can be retrieved by calling AsyncAcceptSocket()
' NOTE: After retrieving the connectedSocket, the next call to AsyncAcceptSocket
' will return Nothing. The connected socket can only be retrieved once.
Set connectedSocket = accSocket.AsyncAcceptSocket()
' Your program may now send and receive messages on connectedSocket...
Else
' if not successful, the error log is available in AsyncAcceptLog:
MsgBox accSocket.AsyncAcceptLog
End If
Else
MsgBox "Background accept thread is still running"
End If
End Sub
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.