(Swift) Send Bytes on a Socket Connection
Demonstrates how to send a mixture of binary (non-text) and text bytes on a socket connection.
func chilkatTest() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let socket = CkoSocket()!
// Connect to some host:port
var ssl: Bool = false
var maxWaitMillisec: Int = 20000
var port: Int = 5555
var success: Bool = socket.connect("test.com", port: port, ssl: ssl, maxWaitMs: maxWaitMillisec)
if success != true {
print("\(socket.lastErrorText!)")
return
}
// We wish to send a 0x00 byte followed by the us-ascii string "10800"
let bd = CkoBinData()!
bd.appendByte(0)
bd.append("10800", charset: "utf-8")
// Send the entire contents of bd.
success = socket.sendBd(bd, offset: 0, numBytes: 0)
if success != true {
print("\(socket.lastErrorText!)")
return
}
}
|