Sample code for 30+ languages & platforms
B4X

Move a Connection into a Socket Set

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.

Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.

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 connection out of connectedSock and into a newly created member of this object's socket
'  set.  After one or more such calls, socketSet acts as a set of connected sockets (useful with the
'  SelectForReading / SelectForWriting methods).  After success, connectedSock holds no connection.
Dim socketSet As ChilkatSocket
socketSet.Initialize("socketSet")
success = socketSet.TakeSocket(connectedSock)
If success = False Then
    Log(socketSet.LastErrorText)
    Return
End If

Log("Connection added to the socket set.")