Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

    listenSock.i = CkSocket::ckCreate()
    If listenSock.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckBindAndListen(listenSock,5000,25)
    If success = 0
        Debug CkSocket::ckLastErrorText(listenSock)
        CkSocket::ckDispose(listenSock)
        ProcedureReturn
    EndIf

    connectedSock.i = CkSocket::ckCreate()
    If connectedSock.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckAcceptNext(listenSock,20000,connectedSock)
    If success = 0
        Debug CkSocket::ckLastErrorText(listenSock)
        CkSocket::ckDispose(listenSock)
        CkSocket::ckDispose(connectedSock)
        ProcedureReturn
    EndIf

    ;  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.
    socketSet.i = CkSocket::ckCreate()
    If socketSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckTakeSocket(socketSet,connectedSock)
    If success = 0
        Debug CkSocket::ckLastErrorText(socketSet)
        CkSocket::ckDispose(listenSock)
        CkSocket::ckDispose(connectedSock)
        CkSocket::ckDispose(socketSet)
        ProcedureReturn
    EndIf

    Debug "Connection added to the socket set."


    CkSocket::ckDispose(listenSock)
    CkSocket::ckDispose(connectedSock)
    CkSocket::ckDispose(socketSet)


    ProcedureReturn
EndProcedure