Sample code for 30+ languages & platforms
C

Poll a Socket for Available Data

See more Socket/SSL/TLS Examples

Demonstrates Socket.PollDataAvailable, which performs a nonblocking check for readable activity and returns whether data or another read-ready condition is present.

Background. This lets an application check for incoming data without blocking, for example in an event loop that services multiple tasks.

Chilkat C Downloads

C
#include <C_CkSocket.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocket socket;
    BOOL bTls;
    int maxWaitMs;
    BOOL bAvailable;

    success = FALSE;

    socket = CkSocket_Create();

    //  Connect to the server using TLS.
    bTls = TRUE;
    maxWaitMs = 5000;
    success = CkSocket_Connect(socket,"example.com",5000,bTls,maxWaitMs);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        return;
    }

    //  Perform a nonblocking check for readable data.  Returns TRUE when data or another read-ready
    //  condition is present, and FALSE when nothing is immediately available.
    bAvailable = CkSocket_PollDataAvailable(socket);
    if (bAvailable != TRUE) {
        printf("No data is immediately available.\n");
    }
    else {
        printf("Data is available to read.\n");
    }



    CkSocket_Dispose(socket);

    }