Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkSocketW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocketW listenSock;
    HCkSocketW connectedSock;
    HCkSocketW handlerSock;

    success = FALSE;

    listenSock = CkSocketW_Create();

    success = CkSocketW_BindAndListen(listenSock,5000,25);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(listenSock));
        CkSocketW_Dispose(listenSock);
        return;
    }

    connectedSock = CkSocketW_Create();
    success = CkSocketW_AcceptNext(listenSock,20000,connectedSock);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(listenSock));
        CkSocketW_Dispose(listenSock);
        CkSocketW_Dispose(connectedSock);
        return;
    }

    //  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 = CkSocketW_Create();
    success = CkSocketW_TakeConnection(handlerSock,connectedSock);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(handlerSock));
        CkSocketW_Dispose(listenSock);
        CkSocketW_Dispose(connectedSock);
        CkSocketW_Dispose(handlerSock);
        return;
    }

    wprintf(L"Connection moved to the handler socket.\n");


    CkSocketW_Dispose(listenSock);
    CkSocketW_Dispose(connectedSock);
    CkSocketW_Dispose(handlerSock);

    }