簡體   English   中英

通過Java套接字發送數據的編碼問題?

[英]Encoding issue for sending data via java socket?

我有以下代碼。 我可以很好地讀取並將數據轉換為十六進制。 問題是當我發回String replyMessage = "7E81"; ,設備將其接收為"3745" 怎么了? 是因為我的編碼,還是我需要做一些轉換后再發送回去?

w =  new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream(),"ISO-8859-15")); //
r = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream(),"ISO-8859-15"));
int nextChar=0;
while ((nextChar=r.read()) != -1) {               
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toHexString(nextChar));
    if (sb.length() < 2) {
        sb.insert(0, '0'); // pad with leading zero if needed
    }
    String hexChar = sb.toString();
    System.out.println("\n\n hex value is "+Integer.toHexString(nextChar).toUpperCase()+"   "+"Int value is:"+nextChar);          
    message = message+hexChar; 
    String messageID=message.substring(2,6);
    System.out.println("messageId is :"+messageID);
    if(messageID.equals("0100")){
        String replyMessage = "7E81";
        w.write(replyMessage+"\r\n");
        w.flush();
    }
}

基於聊天中的評論:

該文件說

起始字節(1字節)7e
訊息編號(2位元組)01 00
郵件正文性質(2字節)00 19
電話號碼。 設備字節數(6字節)09 40 27 84 94 70
消息序列號(2字節)00 01
郵件正文(N字節)00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 40 27 84 94 70 00
校驗碼(1Byte)19
結束字節(1Byte)7E

所以開始和終止是7E

傳出

起始字節(1字節)7e
訊息編號(2位元組)81 00
郵件正文性質(2字節)00 13
電話號碼。 設備字節數(6字節)09 40 27 84 94 70
消息序列號(2字節)00 01
郵件正文(N字節)00 01 00 32 30 31 31 31 31 30 38 31 31 33 33 32 31 39 36
校驗碼(1Byte)9A
結束字節(1Byte)7e

這意味着所討論的協議是二進制協議,而不是您所想的發送十六進制字符串的文本協議。 因此,對於該協議,您對OutputStreamWriterInputStreamReaderStringBuildertoHexString()等的使用都是完全錯誤的。

每條接收和發送的消息均以固定的13字節標頭開頭,后跟可變長度的正文(標頭指定正文長度),並以固定的2字節頁腳結尾。

考慮到這一點,請嘗試以下更類似的方法:

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

...

w = new DataOutputStream(new BufferedOutputStream(receivedSocketConn1.getOutputStream()));
r = new DataInputStream(new BufferedInputStream(receivedSocketConn1.getInputStream()));

...

if (r.readByte() != 0x7E) // start byte
{
    // ah oh, something went wrong!!
    receivedSocketConn1.close();
    return;
}

int messageID = r.readUnsignedShort();     // message ID
int bodyLen = r.readUnsignedShort();       // message body nature (body length)
byte[] phoneNum = new byte[6];
r.readFully(phoneNum);                     // device phone number
int serialNum = r.readUnsignedShort();     // message serial number
byte[] messageBody = new byte[bodyLen];    // message body
r.readFully(messageBody);
byte checkCode = r.readByte();             // check code

if (r.readByte() != 0x7E) // end byte
{
    // ah oh, something went wrong!!
    receivedSocketConn1.close();
    return;
}

// TODO: validate checkCode if needed...

System.out.println("messageId is : 0x" + Integer.toHexString(messageID));
System.out.println("phoneNum is : " + bytesToHex(phoneNum));
System.out.println("serialNum is : 0x" + Integer.toHexString(serialNum));
System.out.println("messageBody is : " + bytesToHex(messageBody));

// process message data as needed...

switch (messageID)
{
    case 0x100:
    {
        // ...

        byte[] replyBody = new byte[19];
        replyBody[0] = 0x00;
        replyBody[1] = 0x01;
        replyBody[2] = 0x00;
        replyBody[3] = 0x32;
        // and so on...

        checkCode = 0x9A; // calculate as needed...

        w.writeByte(0x7e);               // start byte
        w.writeShort(0x8100);            // message ID
        w.writeShort(replyBody.length);  // message body nature (body length)
        w.write(phoneNum);               // device phone number
        w.writeShort(0x0001);            // message serial number
        w.write(replyBody);              // message body
        w.writeByte(checkCode);          // check code
        w.writeByte(0x7e);               // end byte

        break;
    }

    // other message IDs as needed...
}

w.flush();

暫無
暫無

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

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