您好我正在尝试将消息发送到无法从 iOS 设备更改的 Java 服务器。我正在使用 AsyncSocket 并且想知道如何发送和接收附加到字符串的长度。我正在将字符串 UTF 转换为 NSData 但是我想知道这两种语言之间的原语大小是否存在差异。还有大端和小端的变化吗?基本上我需要能够转换以下java方法:
inStream.readUTF();
inStream.readInt();
inStream.readChar();
inStream.readShort();
inStream.readFully(recvBuff, 0, recvLen);
outStream.writeInt();
outStream.writeUTF();
outStream.writeChars();
outStream.writeShort();
outStream.write(sendBytes, 0, sendBytes.length);
我知道我非常接近,但有些地方不太对劲,这就是我到目前为止所得到的:
我正在使用 NSMutableArray 来附加数据并使用 AsyncSockets 读取和写入方法。
[theSocket readDataToData:[AsyncSocket ZeroData] withTimeout:timeout buffer:buffer bufferOffset:offset tag:tag]; // inStream.readUTF();
[theSocket readDataToLength:sizeof(int32_t) withTimeout:timeout buffer:buffer bufferOffset:offset tag:tag]; // inStream.readInt();
[theSocket readDataToLength:sizeof(unichar) withTimeout:timeout buffer:buffer bufferOffset:offset tag:tag]; // inStream.readChar();
[theSocket readDataToLength:sizeof(int16_t) withTimeout:timeout tag:tag]; // inStream.readShort();
[theSocket readDataWithTimeout:timeout buffer:buffer bufferOffset:offset maxLength:maxLength tag:tag]; // inStream.readFully(recvBuff, 0, recvLen);
[outputBufferStream appendBytes:&[sendString length] length:sizeof([sendString length])]; // outStream.writeInt();
[outputBufferStream appendData:[sendString dataUsingEncoding:NSUTF8StringEncoding]] // outStream.writeUTF();
char array[5];
[outputBufferStream appendBytes:array length:sizeof(array)]; // outStream.writeChars();
int16_t _short;
[outputBufferStream appendBytes:&_short length:sizeof(_short)]; // outStream.writeShort();
unsigned char *sendBytes;
[outputBufferStream appendBytes:sendBytes length:sendBytesLength]; // outStream.write(sendBytes, 0, sendBytes.length);
我通常在开头附加长度,如下所示:
int32_t sendStringLength = [sendString length];
[outputBufferStream appendBytes:&sendStringLength length:sizeof(sendStringLength)];
在写入结束时,我将以下内容作为终止符附加:
[outputBufferStream appendData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
我真的很感激这方面的任何帮助。谢谢。
编辑::
多亏了 Robadob,我已经完成了大部分工作。这是我目前试图在 Objective-C 上工作的一点 Java 片段(工作):
private int sendData(String stringToSend) {
if (theSocket==null) {
lastError="sendData() called before socket was set up.";
return 1; // Error
}
try {
System.out.println("Sending "+stringToSend.length()+" chars ["+ stringToSend.length()*2+" bytes]");
System.out.println("'" + stringToSend + "'");
outStream.writeInt(stringToSend.length()*2);
outStream.writeChars(stringToSend);
outStream.flush();
} catch (IOException e) {
lastError="sendData() exception: "+e;
System.out.println(lastError);
return 2; // Error
}
return 0; // Ok
}
这是到目前为止我在 Objective-C 中得到的一个片段:
- (int32_t)sendData:(NSString *)stringToSend {
if (theSocket == nil) {
lastError = @"sendData called before socket was set up";
return 1; // Error
}
@try {
NSLog(@"Sending %d chars [%d bytes]", [stringToSend length], ([stringToSend length] * 2));
NSLog(@"'%@'", stringToSend);
uint32_t stringToSendInt = ([stringToSend length] * 2);
uint32_t stringToSendIntBigE = CFSwapInt32HostToBig(stringToSendInt);
[outputBufferStream appendBytes:&stringToSendIntBigE length:sizeof(stringToSendIntBigE)];
stringToSend = [stringToSend stringByAppendingString:@"\n"];
for (int i = 0; i < ([stringToSend length]); i++) {
unichar characterTmp = [stringToSend characterAtIndex:i];
unichar character = characterTmp << 8;
[outputBufferStream appendBytes:&character length:sizeof(character)];
}
[self syncWriteData:outputBufferStream withTimeout:socketTimeout tag:kSendDataSocketTag];
outputBufferStream = [NSMutableData data];
}
@catch (NSException *e) {
lastError = [NSString stringWithFormat:@"sendData exception: %@", [e reason]];
NSLog(@"%@", lastError);
return 2; // Error
}
return 0; // Ok
}