Search code examples
swiftasyncsocket

How to effectively represent htonl in swift?


I tried to use the socket to transfer large files, and the backend agreed on the transfer method. Objective C can pass the length correctly to the server, but I can't pass the correct length on Swift.

I will show my two pieces of code below.

Objective C

NSMutableData *sendData = [NSMutableData data];
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
uint8_t array[] = {1};
NSData *typeData = [[NSData alloc] initWithBytes:array length:sizeof(array)];
[sendData appendData:typeData];
uint32_t length = htonl(data.length);
NSData *lengthData = [[NSData alloc] initWithBytes:&length length:sizeof(int)];
[sendData appendData:lengthData];
[sendData appendData:data];
self.client.userData = fileId;

[self.client writeData:sendData withTimeout:-1 tag:11];

Swift

let sendData = NSMutableData()
let data = messageField.text!.data(using: .utf8)
let typeData = Data.init(bytes: [1], count: MemoryLayout<Int>.size)
sendData.append(typeData)

let length: UInt32 = 20
var convertedNumber: UInt32 = length.bigEndian
let lengthData = Data.init(bytes: &convertedNumber, count: MemoryLayout<UInt32>.size)
sendData.append(lengthData)
sendData.append(data!)

clientSocket.write(sendData as Data, withTimeout: -1, tag: 0)

Java

byte type = in.readByte();
int length = in.readInt();

Objective C code is OK, how can I convert it to swift.


Solution

  • length.bigEndian is the correct method to convert an integer to its big-endian representation. But there is an error at

    let typeData = Data.init(bytes: [1], count: MemoryLayout<Int>.size)
    

    which creates 8 bytes data (most of which are undefined).

    Note that the same can be achieved more directly, without using NSMutableData. Here is an example:

    let message = "abc"
    let msgData = Data(message.utf8)
    
    var sendData = Data()
    // Append type:
    sendData.append(1)
    // Append message length:
    let length = UInt32(msgData.count).bigEndian
    withUnsafeBytes(of: length) { sendData.append(contentsOf: $0) }
    // Append message:
    sendData.append(msgData)
    
    print(sendData as NSData) // <01000000 03616263>