Sample code for 30+ languages & platforms
Unicode C

Move a Connection into a Socket Set

See more Socket/SSL/TLS Examples

Demonstrates Socket.TakeSocket, which moves a connection into a newly created member of this object's socket set. After one or more calls, the object acts as a set of sockets.

Background. A socket set groups multiple connections so they can be monitored together with the SelectForReading and SelectForWriting methods. After success, the source socket holds no connection.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSocketW.h>

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

    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 connection out of connectedSock and into a newly created member of this object's socket
    //  set.  After one or more such calls, socketSet acts as a set of connected sockets (useful with the
    //  SelectForReading / SelectForWriting methods).  After success, connectedSock holds no connection.
    socketSet = CkSocketW_Create();
    success = CkSocketW_TakeSocket(socketSet,connectedSock);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(socketSet));
        CkSocketW_Dispose(listenSock);
        CkSocketW_Dispose(connectedSock);
        CkSocketW_Dispose(socketSet);
        return;
    }

    wprintf(L"Connection added to the socket set.\n");


    CkSocketW_Dispose(listenSock);
    CkSocketW_Dispose(connectedSock);
    CkSocketW_Dispose(socketSet);

    }