Sample code for 30+ languages & platforms
B4X

Move a Connection into Another Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeConnection, which moves the active connection from one Socket into another. The receiving object first releases any connection it previously held.

Background. Unlike TakeSocket, TakeConnection does not add a member to a socket set. After success, the source socket holds no connection. This is useful for handing an accepted connection to a dedicated handler object.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim listenSock As ChilkatSocket
listenSock.Initialize("listenSock")

success = listenSock.BindAndListen(5000, 25)
If success = False Then
    Log(listenSock.LastErrorText)
    Return
End If


Dim connectedSock As ChilkatSocket
connectedSock.Initialize("connectedSock")
success = listenSock.AcceptNext(20000, connectedSock)
If success = False Then
    Log(listenSock.LastErrorText)
    Return
End If


'  Move the active connection out of connectedSock and into another Socket object.  Unlike
'  TakeSocket, this does not add a member to a socket set.  After success, connectedSock holds no
'  connection.
Dim handlerSock As ChilkatSocket
handlerSock.Initialize("handlerSock")
success = handlerSock.TakeConnection(connectedSock)
If success = False Then
    Log(handlerSock.LastErrorText)
    Return
End If

Log("Connection moved to the handler socket.")