我正在嘗試使用此代碼從設(shè)備接收字符串: byte[] buf = new byte[4]; int read = inFromDevice.read(buf); Logger.getLogger(Utill.class.getName() + " DEBUG_ERR01").log(Level.INFO, "Bytes read: {0}", read); int msgLength = ByteBuffer.wrap(buf).getInt(); Logger.getLogger(Utill.class.getName() + " DEBUG_ERR01").log(Level.INFO, "Message length: {0}", msgLength); Reader r = new InputStreamReader(inFromDevice); char[] cb = new char[msgLength]; int actualCharsRead = r.read(cb); Logger.getLogger(Utill.class.getName() + " DEBUG_ERR01").log(Level.INFO, "Actual chars read: {0} char array length: {1}", new Object[]{actualCharsRead, cb.length}); String msgText = String.valueOf(cb, 0, cb.length); Logger.getLogger(Utill.class.getName() + "Messages Loggining recieve: ").log(Level.INFO, msgText); return msgText;從設(shè)備中和輸入流從接受的服務(wù)器插槽中獲取。代碼大部分時(shí)間都在工作并返回消息,但有時(shí)我收到的消息小于msgLength(根據(jù)協(xié)議是錯(cuò)誤的)日志中的一個(gè)示例是Actual chars read: 1020 char array length: 1391我認(rèn)為由于網(wǎng)絡(luò)問(wèn)題或設(shè)備出現(xiàn)問(wèn)題,問(wèn)題是外部的,但我需要一些專家見解。Java中是否有任何已知問(wèn)題可能導(dǎo)致這種情況?
從套接字使用輸入流讀取器接收 utf-8 字符串?
鴻蒙傳說(shuō)
2022-09-28 10:28:26