Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

    socket.i = CkSocket::ckCreate()
    If socket.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect to the server using TLS.
    bTls.i = 1
    maxWaitMs.i = 5000
    success = CkSocket::ckConnect(socket,"example.com",5000,bTls,maxWaitMs)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ;  Perform a nonblocking check for readable data.  Returns 1 when data or another read-ready
    ;  condition is present, and 0 when nothing is immediately available.
    bAvailable.i = CkSocket::ckPollDataAvailable(socket)
    If bAvailable <> 1
        Debug "No data is immediately available."
    Else
        Debug "Data is available to read."
    EndIf



    CkSocket::ckDispose(socket)


    ProcedureReturn
EndProcedure