buffered Reader server while(in.read() != -1)

Asked

Viewed 56 times

-1

When I am connected by the browser on localhost:80 on the server whose Java code is below, the server reads the request data using class methods BufferedReader.

However, the server is displaying the request data with one letter of each line missing. For example, "GET" is like "ET" and "host" is like "Ost". Here’s an example of what he captured in a request:

ET / HTTP/1.1
ost: localhost
onnection: keep-alive
pgrade-Insecure-Requests: 1
ser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36
ec-Fetch-Dest: document
ccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
ec-Fetch-Site: none
ec-Fetch-Mode: navigate
ec-Fetch-User: ?1
ccept-Encoding: gzip, deflate, br
ccept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7

Here is the corresponding code:

package server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;

public class BufferedReaderServer {

    public static void main(String args[]) throws IOException {

        ServerSocket server = new ServerSocket(80);
        System.out.println("Server");



        while (true) {
            try {

            Socket client = server.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 





            PrintWriter print = new PrintWriter(client.getOutputStream());
            //print.println(abc);

            while(in.read() != -1) {
                System.out.println(in.readLine());
            }



            }catch(Exception e) {e.printStackTrace();}
        }

    }

}
  • I already know where the problem is and how to fix it and I’m just waiting to reopen the question to post an answer.

  • 1

    Ah, and of course, if you had described the problem clearly and prized by a good essay, they would not have closed the question.

  • communities seem to dislike abstract and objective questions with an end. that was the only question I was asked and that was because it was only 1 quick mistake; I had done int input; while(input = i.read()) != -1) { System.out.print(input + ":"); w. println(input); }

  • what I really wanted to know is why I can’t give Printwriter on a Buffered readline like this: Bufferedreader in = new Bufferedreader(new Inputstreamreader(client.getInputStream()); String input; while(input = in.readline()) != null) { System.out.println(input); w. println(input); }

  • I have 1001 questions, but they exonerate me from all communities, give me a negative vote, say I’m incited when I don’t even know what it means; Why does google functions use nodejs and not java? because mysql does not allow you to easily block phpmyadmin without firewall? because there is an all-in-one solution of a server-free java database with cluster cache option and Gui for cross-platform portability so you don’t have to configure a thousand times. or multi btach shell in java to control instances and open all instances without having to decide between shell and batch and compile2x

  • Easy boy. No one here is attacking you. I was just helping you. So much so that I was the one who managed to get your question reopened and even gave her an answer.

  • Stackoverflow has a way of working that is very different from other websites, and this has always generated a lot of friction. Until you get used to how things work, it’s kind of hard. What happens is that questions that are excessively open and with few details hardly have enough information to be answered with any reasonable answer, so they are closed. But this is something simple to solve, just put a little effort in the writing of the question and try to put in them all the necessary information, and the question will be accepted.

  • Ah, and as for the votes, it’s now +1/-2. +1 is mine.

Show 3 more comments

1 answer

1

The problem is that when you give the in.read(), you consume a character of the line to be read and store it nowhere, and therefore it ends up being lost.

The solution would be to do the following:

package server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;

public class BufferedReaderServer {
    public static void main(String args[]) throws IOException {
        ServerSocket server = new ServerSocket(80);
        System.out.println("Server");

        while (true) {
            try {
                Socket client = server.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
                PrintWriter print = new PrintWriter(client.getOutputStream());
                //print.println(abc);

                while (true) {
                    String linha = in.readLine();
                    if (linha == null) break;
                    System.out.println(linha);
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

This takes advantage of the fact that the readLine() returns null when the input a ends and returns some string while there is input to be consumed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.