Sample code for 30+ languages & platforms
PureBasic

Duplicate a Socket Wrapper Sharing the Connection

See more Socket/SSL/TLS Examples

Demonstrates Socket.DupSocket, which populates a destination Socket with another wrapper that shares this object's underlying connection.

Background. The shared connection is reference-counted, and all wrappers operate on the same live connection. The destination first releases any connection it previously held.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ;  Connect to the server using TLS.
    bTls.i = 1
    maxWaitMs.i = 5000
    success = CkSocket::ckConnect(socket,"example.com",5000,bTls,maxWaitMs)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ;  Create a second Socket wrapper that shares this object's underlying connection.  Both wrappers
    ;  operate on the same live connection; the shared connection is reference-counted.
    dupSock.i = CkSocket::ckCreate()
    If dupSock.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckDupSocket(socket,dupSock)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        CkSocket::ckDispose(dupSock)
        ProcedureReturn
    EndIf

    Debug "Duplicate socket wrapper created."


    CkSocket::ckDispose(socket)
    CkSocket::ckDispose(dupSock)


    ProcedureReturn
EndProcedure