【问题标题】:Bufferedreader stops readingBufferedreader 停止读取
【发布时间】:2019-08-26 19:07:03
【问题描述】:

读完整个字符串后,读卡器卡在while,甚至不抛出异常。我正在通过 curl 向服务器发送请求。

我尝试更改 curl 的 Content-Type,更改字符串的内容,并使用另一种方式读取输入,例如扫描仪,但总是卡在 while。

curl -H "Content-Type: text/plain" -d "asdasdasdashdiasdasgdasgduascuyasvccccc" 192.168.0.59:8080
    // Read characters from the client via input stream on the socket
    in = new BufferedReader(new 
    InputStreamReader(connect.getInputStream()));
    // Get character output stream to client (for headers)
    out = new PrintWriter(connect.getOutputStream());
    // Get binary output stream to client (for requested data)
    dataOut = new BufferedOutputStream(connect.getOutputStream());

    // Get first line of the request from the client
    input = in.readLine();
    // Parse the request with a string tokenizer
    if (input == null || input.isEmpty()) {
        System.err.println("Invalid input");
        return;
    }

    parse = new StringTokenizer(input);

    method = parse.nextToken().toUpperCase();
    input = in.readLine();

    do {

        parse = new StringTokenizer(input);
        if (parse.hasMoreTokens()) {
            header = parse.nextToken().toUpperCase(); // we get the 
        HTTP method of the client
        }
        if (parse.hasMoreTokens()) {
            hValue = parse.nextToken().toLowerCase();
        }

        // Support only GET and HEAD methods, we check
        out.println(header + hValue);
        System.out.println(header + hValue);
        if (header.equals("CONTENT-TYPE:")) {
            readFile(in);
        }
        input = in.readLine();
    } while (!input.isEmpty());

    private void readFile(BufferedReader file) throws IOException {
        try {
            int count;
            count = 0;

            while ((count = file.read()) != -1) {
                System.out.print((char) count);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            file.close();
        }
    }

日志:

Server started.

Listening for connections on port : 8080 ...

Connection opened. (Mon Aug 26 15:44:49 BRT 2019)

HOST:192.168.0.59:8080

USER-AGENT:curl/7.58.0

ACCEPT:*/*

CONTENT-TYPE:text/plain

Content-Length: 39

asdasdasdashdiasdasgdasgduascuyasvccccc

【问题讨论】:

    标签: java curl server inputstream bufferedreader


    【解决方案1】:

    似乎您的客户端(curl)没有关闭连接,因此

    (count = file.read()) != -1
    

    在读取最后一个字符后,计数被反复设置为 0,因此不断返回 true。

    您需要确保客户端关闭连接,或者将消息长度与消息一起发送,并在收到发送的字符数时将其关闭服务器端(在 java 中)。

    后者可以这样做:

    1) 发送

    curl -H "Content-Type: text/plain" -d "5asdas" 192.168.0.59:8080
    

    2)更新代码先读取长度,然后倒计时到0,像这样关闭连接

    private void readFile(BufferedReader file) throws IOException {
        int expectedCount = -1;
        try {
                int count;
                count = 0;
    
                while ((count = file.read()) != -1 && (expectedCount > 0 || expectedCount == -1 ) ) {
                    if ( expectedCount == -1 ) {
                        // read count and set in expectedCount
                    } else {
                        //do what you need to do with the input
                        System.out.print((char) count);
                    }
                }
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                file.close();
            }
        }
    

    请注意,此解决方案不会考虑多位数长度之类的大量内容,但应该足以让您继续前进。

    【讨论】:

    • 就像你说的,我得到内容长度并使用一个变量辅助来计算实际索引是否大于零,并且有效,但我认为这是解决错误的一种方法,而不是修复它。
    • 现在我明白你想对我说什么 kkk,我正在同时阅读标题和客户端发送的内容,为了修复它,我只是先阅读标题和他们再过一会儿,然后使用 readFile 方法,就像您使用相同的 BufferedReader 使用标题部分中提供的实际内容长度一样。简单来说,首先我们需要读取头部,然后读取客户端发送的内容。
    【解决方案2】:

    如果使用的是 pre-Java8,你需要处理 BufferedReaderclose 和相关的 I/O 类属性。

    【讨论】:

    • 在catch标签之后我在代码中调用close方法,“finally { file.close(); }”,如果是你在说的,读完所有字符串后仍然卡住
    • 似乎有人已经指出了根本原因your client (curl) isn't closing the connection
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多