Blast
Blast

Reputation: 23

BufferedReader isn't reading whole string

I'm playing around with sockets, but seem to have an issue with the Buffered-Reader not reading the whole string after I enter a space (e.g. "hi there", it will only print "hi") has anyone else had this issue before?

    Server e = new Server();

    e.start();
    ServerSocket serverSocket = new ServerSocket(41021);
    Socket socket = serverSocket.accept();
    System.out.println("Server: (Network.Client) connected");
    InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());


    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);




        System.out.println("Client: " + bufferedReader.readLine());


        serverSocket.close();
        socket.close();



}


@Override

public void run() {


    try {


        Socket socket = new Socket("localhost", 41021);
        while (socket.isConnected()) {
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream());


            printWriter.println(new Scanner(System.in).next());
            printWriter.flush();


        }


        socket.close();


    } catch (IOException e) {

        e.printStackTrace();
    }

}

}

Upvotes: 2

Views: 100

Answers (1)

Yuri
Yuri

Reputation: 40

I think that this happens because in line printWriter.println(new Scanner(System.in).next()); you using next(); instead of nextLine(); Try nextLine(); and notice me if it is working.

Upvotes: 2

Related Questions