我有一个使用 BufferedOutputStream/BufferedInputStream
发送/接收数据的客户端/服务器应用程序。通信协议(protocol)如下:
发送部分:
- 第一个字节是要执行的 Action
- 接下来的4个字节是消息的长度
- 接下来的 x 个字节(x=消息的长度)是消息本身
接收部分:
- 读取第一个字节以获取 Action
- 读取接下来的 4 个字节以获得消息长度
- 读取 x(在上一步中获得)字节以获取消息
现在的问题是,有时 当我在服务器部分发送消息的长度(例如:23045)时,当我收到它时,我得到一个巨大的整数(例如:123106847)。
一个重要的线索是,当消息超过一定数量的字符(在我的例子中 > 10K)时,这种情况仅发生,如果我发送一个较小的消息(例如 4-5k),一切都按预期工作。
客户端发送部分(outputStream/inputStream都是BufferedXXXStream类型):
private String getResponseFromServer( NormalizerActionEnum action, String message) throws IOException{
writeByte( action.id());
writeString( message);
flush(;
return read();
}
private String read() throws IOException{
byte[] msgLen = new byte[4];
inputStream.read(msgLen);
int len = ByteBuffer.wrap(msgLen).getInt();
byte[] bytes = new byte[len];
inputStream.read(bytes);
return new String(bytes);
}
private void writeByte( byte msg) throws IOException{
outputStream.write(msg);
}
private void writeString( String msg) throws IOException{
byte[] msgLen = ByteBuffer.allocate(4).putInt(msg.length()).array();
outputStream.write(msgLen);
outputStream.write(msg.getBytes());
}
private void flush() throws IOException{
outputStream.flush();
}
Server部分(_input/_output都是BufferedXXXStream类型)
private byte readByte() throws IOException, InterruptedException {
int b = _input.read();
while(b==-1){
Thread.sleep(1);
b = _input.read();
}
return (byte) b;
}
private String readString() throws IOException, InterruptedException {
byte[] msgLen = new byte[4];
int s = _input.read(msgLen);
while(s==-1){
Thread.sleep(1);
s = _input.read(msgLen);
}
int len = ByteBuffer.wrap(msgLen).getInt();
byte[] bytes = new byte[len];
s = _input.read(bytes);
while(s==-1){
Thread.sleep(1);
s = _input.read(bytes);
}
return new String(bytes);
}
private void writeString(String message) throws IOException {
byte[] msgLen = ByteBuffer.allocate(4).putInt(message.length()).array();
_output.write(msgLen);
_output.write(message.getBytes());
_output.flush();
}
....
byte cmd = readByte();
String message = readString();
任何帮助将不胜感激。如果您需要更多详细信息,请告诉我。
更新:由于 Jon Skeet 和 EJP 的评论,我意识到服务器上的读取部分进行了一些毫无意义的操作,但撇开这个不谈,我终于明白了问题所在:关键是我在应用程序的整个长度和前几次发送消息时保持流打开我可以在服务器端读取它的长度,但是因为 Jon Skeet 指出数据不会一次全部到达,所以当我尝试读取消息长度时我再次实际上是从消息本身读取的,这就是为什么我有虚假的消息长度。
~ 我没有发送数据长度然后一次读取所有数据,而是发送了没有长度的数据,我一次读取一个字节,直到字符串的末尾,这完美地工作
private String readString() throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
byte[] bytes = new byte[100];
int s = 0;
int index=0;
while(true){
s = _input.read();
if(s == 10){
break;
}
bytes[index++] = (byte) (s);
if(index == bytes.length){
sb.append(new String(bytes));
bytes = new byte[100];
index=0;
}
}
if(index > 0){
sb.append(new String(Arrays.copyOfRange(bytes, 0, index)));
}
return sb.toString();
}
最佳答案
看看这个:
byte[] bytes = new byte[len];
s = _input.read(bytes);
while(s==-1){
Thread.sleep(1);
s = _input.read(bytes);
}
return new String(bytes);
首先,循环是没有意义的:read
唯一会返回 -1 的情况是它已关闭,在这种情况下循环不会帮助您。
其次,您忽略了数据将以多个 block 的形式出现的可能性。您假设如果您设法获得了任何 数据,那么您就获得了所有 数据。相反,你应该像这样循环:
int bytesRead = 0;
while (bytesRead < bytes.length) {
int chunk = _input.read(bytes, bytesRead, bytes.length - bytesRead);
if (chunk == -1) {
throw new IOException("Didn't get as much data as we should have");
}
bytesRead += chunk;
}
请注意,您所有的other InputStream.read
调用还假定您已成功读取数据,并且确实已读取所有 你需要的数据。
哦,您正在使用平台默认编码在二进制数据和文本数据之间进行转换 - 这不是个好主意。
您有什么理由不为此使用 DataInputStream
和 DataOutputStream
吗?目前,您正在重新发明轮子,并且会遇到错误。
关于java - 使用BufferedOutputStream/BufferedInputStream的socket随机接收虚假数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17185384/