Swift
Swift
Receive Bytes up to a Delimiter into a BinData
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveUntilByteBd, which reads bytes until a delimiter byte is encountered and appends all bytes up to and including the delimiter to a BinData.
Background. The delimiter is a raw byte value from 0 through 255. This is useful for framing schemes that terminate each message with a known delimiter byte.
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
}
// Read bytes until the delimiter byte is encountered, appending all bytes up to and including the
// delimiter to a BinData. The delimiter is a raw byte value from 0 through 255 (here the linefeed
// byte, decimal 10).
let bd = CkoBinData()!
success = socket.receive(untilByteBd: 10, bd: bd)
if success == false {
print("\(socket.lastErrorText!)")
return
}
print("Received \(bd.numBytes.intValue) bytes (including the delimiter).")
}