Swift
Swift
ReceiveString Returns Only the Immediately Available Data
See more Socket/SSL/TLS Examples
Demonstrates that Socket.ReceiveString returns only the text immediately available and not the entire response. The example connects to chilkatsoft.com over TLS and requests big_helloWorld.txt, a large text file of roughly 74,000 lines. A single ReceiveString call returns just the first portion of the response, and the example reports how many characters were received compared with the total size given by the response Content-Length header.
Background. Because ReceiveString returns whatever text is currently available, one call cannot indicate whether the full response has arrived. Knowing when a response is complete requires either a known expected length (such as a Content-Length header or an application-defined length prefix), receiving in a loop until that many bytes are collected, or receiving until the peer closes the connection. This example sends a minimal HTTP request only to exercise ReceiveString against a public server; for actual HTTP work, use the Chilkat Http, Rest, or HttpCurl classes.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
let socket = CkoSocket()!
// Connect to chilkatsoft.com over TLS.
var bTls: Bool = true
var maxWaitMs: Int = 5000
success = socket.connect(hostname: "chilkatsoft.com", port: 443, ssl: bTls, maxWaitMs: maxWaitMs)
if success == false {
print("\(socket.lastErrorText!)")
return
}
socket.stringCharset = "utf-8"
// Send an HTTP GET request for big_helloWorld.txt, a large text file of roughly 74,000 lines of
// "Hello World!". This uses a minimal HTTP request only to exercise ReceiveString against a public
// server -- for real HTTP work, use the Chilkat Http, Rest, or HttpCurl classes instead.
var httpReq: String? = "GET /big_helloWorld.txt HTTP/1.1\r\nHost: chilkatsoft.com\r\nConnection: close\r\n\r\n"
success = socket.sendString(str: httpReq)
if success == false {
print("\(socket.lastErrorText!)")
return
}
// Call ReceiveString a single time. It returns only the text that is immediately available --
// typically just the first network packet -- and does NOT wait for the entire response to arrive.
var responseText: String? = socket.receiveString()
if socket.lastMethodSuccess == false {
print("\(socket.lastErrorText!)")
return
}
// Load the received text into a StringBuilder so its length can be measured and the Content-Length
// header extracted. The Content-Length header gives the total size, in bytes, of the response body.
let sb = CkoStringBuilder()!
sb.append(value: responseText)
var contentLength: String? = sb.getBetween(beginMark: "Content-Length: ", endMark: "\r\n")
if sb.lastMethodSuccess == false {
print("\(sb.lastErrorText!)")
return
}
// Compare what this single ReceiveString returned against the full size of the expected response.
// The received amount is only a small fraction of the total.
print("Characters received so far (headers + partial body): \(sb.length.intValue)")
print("Expected response body size (Content-Length): \(contentLength!) bytes")
// A single ReceiveString call returns whatever is currently available, so it cannot indicate whether
// the full response has arrived. This makes it difficult to know when a response is complete: the
// application must either know how many bytes to expect (for example from a Content-Length header or
// an application-defined length prefix) and keep receiving until that many bytes are collected, or
// keep receiving until the peer closes the connection.
}