嗨,我正在尝试向Java服务器发送无法从iOS设备更改的消息。我正在使用AsyncSocket,想知道如何发送和接收附加到字符串的长度。我正在进行字符串到NSData的UTF转换,但我想知道这两种语言之间的原语大小是否存在差异。还有大端序和小端序的变体吗?基本上,我需要能够转换以下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
}
如果您阅读了writeUTF的文档,它会说它使用writeShort写入前2个字节。上面写着
将short作为两个字节(高字节)写入基础输出流第一如果未引发异常,则写入的计数器将递增乘以2。
一个字节是8位,所以他们写入的值是16位,您使用的是int32_t
,即32位。你应该写一个int16
或int16_t
(我不是objective-c)。