Sample code for 30+ languages & platforms
Xbase++

Wait for a Socket to Become Readable

See more Socket/SSL/TLS Examples

Demonstrates Socket.SelectForReading, which waits for read-ready activity on the socket, or on any contained socket when the object is a socket set. It returns 1 if ready, 0 on timeout, or -1 on error.

Background. A timeoutMs of 0 performs a nonblocking poll. With a socket set, this monitors many connections at once for readability.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL nBTls
LOCAL nMaxWaitMs
LOCAL nReady

nSuccess := 0

oSocket := CreateObject("Chilkat.Socket")

//  Connect to the server using TLS.
nBTls := 1
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("example.com", 5000, nBTls, nMaxWaitMs)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

//  Wait up to 5 seconds for read-ready activity on the socket (or, for a socket set, on any contained
//  socket).  Returns 1 if ready, 0 on timeout, or -1 on error.
nReady := oSocket:SelectForReading(5000)
IF (nReady < 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

IF (nReady != 1)
    ? "No data became available before the timeout."
    oSocket:destroy()
    RETURN
ENDIF

? "The socket has data ready to read."

oSocket:destroy()