Sample code for 30+ languages & platforms
Swift

Send Bytes on a Socket Connection

See more Socket/SSL/TLS Examples

Demonstrates how to send a mixture of binary (non-text) and text bytes on a socket connection.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // 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
    success = socket.connect(hostname: "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(byteValue: 0)
    bd.appendString(str: "10800", charset: "utf-8")

    // Send the entire contents of bd.
    success = socket.sendBd(binData: bd, offset: 0, numBytes: 0)
    if success != true {
        print("\(socket.lastErrorText!)")
        return
    }


}