在 java 中从套接字流中读取文本和二进制文件时出现奇怪的字符

weird characters while reading text and binary from a socket's stream in java

我曾尝试寻找答案,但没有成功(在 google 和 Whosebug)

我正在编写一个 java 程序,其中服务器和客户端可以通过 sending/receiving 数据和文件进行通信...

我正在按每个 1MB 的块发送文件。为了让客户端知道块的数量,我发送了一个包含 BlockNb=x 的字符串行,其中 x 是块的数量,后跟文件。

然而,当我从客户端读取它时,我收到的不是这一行,而是一些奇怪的字符:ur\u0000\u0002[B¬ó\u0017ø\bTà\u0002\u0000\u0000xp\u0000\u0000\bP 其中 \b\uxxxx 是它们值的代表(我期待这里 BlockNb=1)

(写得更清楚:ur [B¬ó ø Tà xp P(其中空格是转义字符)

这是我的代码。
服务器端

try (
        ServerSocket welcome = new ServerSocket(6500);
        Socket socket = welcome.accept();
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream())
    )
    {
        System.out.println("accepted");
        File f = new File("...");  //the file path
        try (
            FileInputStream fileInputStream = new FileInputStream(f)
        )
        {
            long length = f.length();
            byte[] buffer;
            //here I put only the code that was executed
            buffer = new byte[(int) length];
            printWriter.println("BlockNb=1");

            fileInputStream.read(buffer);
            outputStream.writeObject(buffer);

            printWriter.println("}");
        }
    }

客户端

try (
        Socket socket = new Socket("localhost", 6500);
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        FileOutputStream fileOutputStream = new FileOutputStream("C:/D/test.txt");
        Scanner scanner = new Scanner(socket.getInputStream())
    )
    {
        String msg = scanner.nextLine();  //the weird string was read at this point
        long blockNb = Long.parseLong(msg.split("BlockNb=", 2)[1]);
        byte[] file = (byte[]) inputStream.readObject();
        fileOutputStream.write(file);
    }

P.S.: 当我从服务器端(仅)删除最后 3 行时,一切正常。正如预期的那样,我收到了 BlockNb=1。所以这个问题只有在混合了两种 data/two 类型的 outputStreams

时才会出现

英语是我的第三语言,如果语法错误或单词误用,请原谅我

编辑:这里的主要问题是我忘记刷新流。但是,当我刷新流时,我开始收到 EOFException(如果我为 read/write 应用排序,我可以在调试中避免这种情况 - 存在一种同步问题)所以我使用了 QuickSilver 的解决方案,它除了刷新之外还有效。

我会避免使用两个不同的对象来写入和读取同一个流。尝试在服务器端使用 ObjectOutputStream,在客户端使用 ObjectInputStream。像处理缓冲区一样处理字符串:

服务器:outputStream.writeObject("BlockNb=1");

客户:String blockNbStr = inputStream.readObject();