こんにちは、iOS デバイスから変更できない Java サーバーにメッセージを送信しようとしています。私は AsyncSocket を使用しており、文字列に追加された長さを送受信する方法を知りたいと思っていました。文字列を NSData に UTF 変換していますが、2 つの言語間でプリミティブのサイズに違いがあるかどうか疑問に思っていました。また、ビッグエンディアンとリトルエンディアンのバリエーションはありますか? 基本的に、次の 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
}