Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkSocket.h>

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

    success = FALSE;

    listenSock = CkSocket_Create();

    success = CkSocket_BindAndListen(listenSock,5000,25);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(listenSock));
        CkSocket_Dispose(listenSock);
        return;
    }

    connectedSock = CkSocket_Create();
    success = CkSocket_AcceptNext(listenSock,20000,connectedSock);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(listenSock));
        CkSocket_Dispose(listenSock);
        CkSocket_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 = CkSocket_Create();
    success = CkSocket_TakeSocket(socketSet,connectedSock);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socketSet));
        CkSocket_Dispose(listenSock);
        CkSocket_Dispose(connectedSock);
        CkSocket_Dispose(socketSet);
        return;
    }

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


    CkSocket_Dispose(listenSock);
    CkSocket_Dispose(connectedSock);
    CkSocket_Dispose(socketSet);

    }