2013-09-21 69 views
0

我想發送一些字節的數據通過我的局域網登錄到嵌入式設備,但是當我發送數據時有2個字節被附加到tcp流。Java,tcp輸出中不需要的數據來自哪裏?

這是什麼造成的?

public class DeviceConnection { 

    Device dv; 
    Socket s; 
    PrintWriter out; 
    boolean connected = true; 

    public DvsConnection(Device d) { 
     try { 
      dv = d; 
      InetAddress addr = InetAddress.getByName(dv.IP_ADDRESS); 
      s = new Socket(addr, dv.ACTIVEX_PORT); 
     } catch (Exception ex) { 
      System.out.println(ex.toString()); 
      connected = false; 
     } 
     Connect(); 
    } 

    private void Connect() { 
     int[] headerStrt = {0x67, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 
     int[] usrName = convert.EncryptStr(15, dv.LOGIN, cypher); 
     int[] password = convert.EncryptStr(15, dv.PASSWORD, cypher); 
     int[] headerEnd = { 
      0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00}; 
     int[] messageArray = convert.concatAll(headerStrt, usrName, spacer2, password, headerEnd); 
     try { 
      out = new PrintWriter(s.getOutputStream(), true); 
      byte[] sbytes=convert.IntArray2ByteArray(messageArray); 
      String message=convert.ByteArray2Ascii(sbytes); 
      out.println(message); 
      String hex = convert.ByteArray2Hex(sbytes); 
      System.out.println(hex); 
     } catch (Exception ex) { 
      System.out.println(ex.toString()); 
      connected = false; 
     } 
    } 


public class Convert { 

    public String ByteArray2Hex(byte[] a) { 
     StringBuilder sb = new StringBuilder(); 
     for (byte b : a) { 
      sb.append(String.format("%02x", b & 0xff)); 
     } 
     return sb.toString(); 
    } 

    public String ByteArray2Ascii(byte[] data) { 
     StringBuilder sb = new StringBuilder(data.length); 
     for (int i = 0; i < data.length; ++i) { 
      if (data[i] < 0) { 
       throw new IllegalArgumentException(); 
      } 
      sb.append((char) data[i]); 
     } 
     return sb.toString(); 
    } 

} 

控制檯輸出:

6745000000000000000000004b4c4c29 
4d15176d0000000000000000005d4c57 
751653543b0808001c00000000001b00 
00000000000000000000000000000000 
00000000000000000000000000000000 
00000000000000000000000000000000 
00000000000000000000000000000000 

Wireshark的輸出:

00000000 67 45 00 00 00 00 00 00 00 00 00 00 4b 4c 4c 29 gE...... ....KLL) 
00000010 4d 15 17 6d 00 00 00 00 00 00 00 00 00 5d 4c 57 M..m.... .....]LW 
00000020 75 16 53 54 3b 08 08 00 1c 00 00 00 00 00 1b 00 u.ST;... ........ 
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........ 
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........ 
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........ 
00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........ 
00000070 0d 0a           .. 
+1

PrintWriter.println在行尾添加回車/換行(0x0D 0x0A)。 –

+0

@AxelKemper解釋它,任何想法我可以剝離他們? – andrew

+0

@andrew:不要使用'println'開頭!但理想情況下,不要使用Writer作爲二進制數據... –

回答

2

這是println被添加\r\n,但是你現在看到的基本問題不止於此。您將二進制數據視爲文本。那早晚咬你。

如果您嘗試使用二進制數據,請使用OutputStreamInputStream的子類 - 而不是WriterReader的子類。你已經從套接字獲取流 - 你爲什麼要把它們包裝在PrintWriterPrintReader?這是一個根本性的錯誤,你應該立即修復

一旦你停止這樣做,你不會因爲額外的「換行符」數據而結束,因爲當你不考慮文本時,不會有換行符。有關詳情,請參閱Marc Gravell's blog post on IO mistakes

此外,我會強烈勸你開始下面的Java命名約定 - 方法camelCased,不PascalCased。我希望在真正的代碼中你的異常處理也有所不同:)