Sample code for 30+ languages & platforms
PureBasic

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

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

    Debug "Connection moved to the handler socket."


    CkSocket::ckDispose(listenSock)
    CkSocket::ckDispose(connectedSock)
    CkSocket::ckDispose(handlerSock)


    ProcedureReturn
EndProcedure