簡體   English   中英

通過AsyncSocket將消息發送到Objective-C中的Java服務器

[英]Sending messages to java server in objective-c via AsyncSocket

嗨,我正在嘗試向無法從iOS設備更改的Java服務器發送消息。 我正在使用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位,因此您正在使用的值int32_t是32位,因此它們正在寫入的值是16位。 您應該編寫一個int16int16_t (我不是Objective-C)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM