【问题标题】:How to include newLine characters that BufferedReader ignores如何包含 BufferedReader 忽略的 newLine 字符
【发布时间】:2017-10-26 04:29:14
【问题描述】:

我需要使用缓冲区读取器通过套接字进行通信,我只是想知道如何保留新行。我希望最终在密码学中使用它,所以我不能假设新行位于消息的末尾。

我是一般套接字的新手,因此我们将不胜感激。

客户端发送的文本

   ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
   Just a pure test to see if my code works. Two chemists walk into a bar...

字符串长度为205

在服务器端我得到

    ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
    Just a pure test to see if my code works. Two chemists walk into a bar...

这个长度是206,就是在末尾加1个换行符。这是在我尝试实施我发现的一些解决方案之后,但由于我偏离了 1 个字符,它们没有工作。 这是读取客户端输入的代码

while (incoming != null)
    {
        /* If the client has sent "exit", instruct the server to
         * remove this thread from the vector of active connections.
         * Then close the socket and exit.
         */
        if (incoming.compareTo("exit") == 0)
        {
            parent.kill (this);
            try {
                in.close ();
                sock.close ();
            }
            catch (IOException e)
            {/*nothing to do*/}
            return;
        }

        /* If the client has sent "die", instruct the server to
         * signal all threads to shutdown, then exit.
         */
        else if (incoming.compareTo("die") == 0)
        {
            parent.killall ();
            return;
        }   

        System.out.println("LINE: " + incoming.length() + " " + incoming);
        if(incoming.equals("3nd0f5krr4hf1l3") && !msgGotten)
        {
            //Decrypt
            SecretKeySpec key = CryptoUtilities.key_from_seed("H".getBytes());

            System.out.print(message);
            System.out.println(message.length());
            byte[] encryptedMessageAndDigest = message.getBytes();
            byte[] decryptedMessageAndDigest = CryptoUtilities.decrypt(encryptedMessageAndDigest, key);

            if(CryptoUtilities.verify_hash(decryptedMessageAndDigest, key))
            {
                byte[] headerMessage = CryptoUtilities.extract_message(decryptedMessageAndDigest);

                String payload = new String(headerMessage);

                String[] plainTextArray = payload.split("3nd0fh34d3rl1n3", 3);
                String fileName = plainTextArray[0];
                byte[] plaintextBytes = plainTextArray[2].getBytes();

                try 
                {
                    FileOutputStream fos = new FileOutputStream(fileName);
                    fos.write(plaintextBytes);
                    fos.close();
                } 
                catch (IOException e) 
                {
                }
            }
            else
            {

            }
        }
        else
        {
            /* Otherwise, just echo what was received. */
            //System.out.println ("Client " + idnum + ": " + incoming);

            if(incoming.length() == 0)
            {
                message = message + "\n";
            }
            else
            {
                message = message + incoming + "\n";
            }

        }

提前致谢!

编辑:在我的初始传输结束时,我发送 3nd0f5krr4hf1l3 表示它。我需要readLine(),所以我可以一口气解析,但如果read() 确实是最好的方法,我将如何实现它? \n (\r\n) 字符会在读取中出现吗?

【问题讨论】:

    标签: java sockets cryptography bufferedreader


    【解决方案1】:

    如果你不能假设有行终止符,显然你不能首先使用readLine()。您应该使用read() 重载之一。

    \n (\r\n) 字符会在阅读中出现吗?

    所有字符都会出现在read()中。

    【讨论】:

    • 对不起,我的意思是,每当换行符出现在原始文本中时,它就变成了分隔标记,但是当标记为 = 换行符时,我似乎无法使其工作。
    • 我无法确定这一点,但如果您想保留行终止符,请不要使用readLine()
    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2021-04-21
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多