gpt4 book ai didi

java - 通过AsyncSocket向objective-c中的java服务器发送消息

转载 作者:行者123 更新时间:2023-12-01 04:31:03 25 4
gpt4 key购买 nike

您好,我正在尝试将消息发送到 Java 服务器,但无法从 iOS 设备更改该消息。我正在使用 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
}

最佳答案

如果您阅读 writeUTF 的文档它说它使用writeShort写入前2个字节。这说的是

Writes a short to the underlying output stream as two bytes, high byte first. If no exception is thrown, the counter written is incremented by 2.

一个字节是 8 位,因此它们写入的值是 16 位,您使用的是 int32_t,它是 32 位。您应该编写 int16int16_t (我不使用 Objective-C)。

关于java - 通过AsyncSocket向objective-c中的java服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com