Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Boolean iBTls
    Integer iMaxWaitMs
    Variant vDupSock
    Handle hoDupSock
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    //  Connect to the server using TLS.
    Move True To iBTls
    Move 5000 To iMaxWaitMs
    Get ComConnect Of hoSocket "example.com" 5000 iBTls iMaxWaitMs To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComChilkatSocket)) To hoDupSock
    If (Not(IsComObjectCreated(hoDupSock))) Begin
        Send CreateComObject of hoDupSock
    End
    Get pvComObject of hoDupSock to vDupSock
    Get ComDupSocket Of hoSocket vDupSock To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Duplicate socket wrapper created."


End_Procedure