简体   繁体   English

获取输入流中 STX 和 ETX 之间的字符串/字节

[英]Get String/Byte between STX & ETX in Inputstream

I'm working on TCP socket but I realize it will only print the message ends with "\n".我正在研究 TCP 套接字,但我意识到它只会打印以“\n”结尾的消息。 The problem is the server will send the message ends with ETX (without "\n") hence I'm having trouble to print the String.问题是服务器将发送以 ETX 结尾的消息(没有“\n”),因此我无法打印字符串。 Is there any way to get data between the STX and ETX?有什么方法可以在 STX 和 ETX 之间获取数据?

this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    rxMsg = input.readLine();
                 
                    Log.v("InputStream",rxMsg);

Though I've not tested your code, the cause lies on your readLine function call.虽然我没有测试你的代码,但原因在于你的readLine function 调用。 readLine does not finish its work until ETX is found. readLine 在找到 ETX 之前不会完成它的工作。

Instead, try using read() or read(char[] cbuf, int off, int len) instead of readLine .相反,请尝试使用read()read(char[] cbuf, int off, int len)而不是readLine These functions receives arbitrary length of bytes (same or shorter than what the sender had sent) sometimes, then you may want to call read again for receiving the remaining data.这些函数有时会接收任意长度的字节(与发送者发送的字节相同或更短),然后您可能需要再次调用read以接收剩余的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM