Writing structure to an outputStream in Swift 3

Viewed 506

I'm trying to pass a structure in a stream so that it is then sent over the socket to another device. The code works, but the wrong data is sent. And each time random data is sent - then I'm doing something wrong. Where am I wrong? Here is my code:

 public struct PStypes {
    var u: UInt32              //< [X_XXXXXX V]
    var i: UInt32              //< [X_XXXXXX A]
   }
 func sendMessage(message: String) {
    var task = PStypes(u: 7, i: 9)
    var bufferData = NSData(bytes: &task, length: 8)
    var data:Data = bufferData as Data
    var bufferDataSize = data.count                
    let bytesWritten = withUnsafePointer(to: &data) {
        $0.withMemoryRebound(to: UInt8.self, capacity: bufferDataSize) {
            outputStream.write($0, maxLength: bufferDataSize)
        }
    }   
}
2 Answers
Related