Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
let socket = CkoSocket()!
// Connect to the server using TLS.
var bTls: Bool = true
var maxWaitMs: Int = 5000
success = socket.connect(hostname: "example.com", port: 5000, ssl: bTls, maxWaitMs: maxWaitMs)
if success == false {
print("\(socket.lastErrorText!)")
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.
var bAvailable: Bool = socket.pollDataAvailable()
if bAvailable != true {
print("No data is immediately available.")
}
else {
print("Data is available to read.")
}
}