Sample code for 30+ languages & platforms
C Requires Chilkat v11.0.0+

Example: Socket.DupSocket method

See more Socket/SSL/TLS Examples

Demonstrates how to call the DupSocket method.

Chilkat C Downloads

C
#include <C_CkSocket.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocket socket;
    HCkSocket socket_for_sending;
    HCkSocket socket_for_monitor;

    success = FALSE;

    socket = CkSocket_Create();
    success = CkSocket_Connect(socket,"example.com",443,TRUE,5000);

    //  ...
    //  ...

    //  Duplicate the socket to share the same connection across threads.
    //  In a multithreaded application, each thread should use its own socket object. For example:

    //  - The main thread can use the original socket for reading.
    //  - A secondary thread can use a duplicate for writing.
    //  - Another thread can use a separate duplicate to monitor the connection status.

    socket_for_sending = CkSocket_Create();
    success = CkSocket_DupSocket(socket,socket_for_sending);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        CkSocket_Dispose(socket_for_sending);
        return;
    }

    socket_for_monitor = CkSocket_Create();
    success = CkSocket_DupSocket(socket,socket_for_monitor);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        CkSocket_Dispose(socket_for_sending);
        CkSocket_Dispose(socket_for_monitor);
        return;
    }

    //  All three socket objects: socket, socket_for_sending, and socket_for_monitor share the same underlying connection.


    CkSocket_Dispose(socket);
    CkSocket_Dispose(socket_for_sending);
    CkSocket_Dispose(socket_for_monitor);

    }