为什么我的BufferedReader没有" Ready"?

时间:2015-02-21 02:08:20

标签: java java.util.scanner bufferedreader

我查看了所有BuffedReader“未准备好”的问题,甚至与我正在寻找的内容略有关系,但我找不到解决问题的方法。

我有一个客户端和服务器使用适当的方法(即BufferedReader,PrintWriter)发送/接收字符串,但我的BufferedReader不起作用。当它遇到if循环,它检查BufferedReader是否准备就绪时,它将转到else语句并执行它的操作。老实说,我不知道如何解决这个问题。

总是感谢帮助,感谢您花时间阅读本文。

客户端代码:

import java.net.*;
import java.util.Scanner;
import java.io.*;
public class clientServer
{
    public static void main (String [] args) throws IOException
    {
        //Opening Statement
        System.out.println("Client/Server Program Utilizing Sockets!");
        System.out.println();

        //Opening/connected socket
        String serverAddress = "127.0.0.1";
        Socket socket = new Socket(serverAddress, 13000);

        //Checks if connection is working
        System.out.print("Connection: ");
        if(socket.isConnected()== true)
        {
            System.out.println("Working...");
            System.out.println();
        }
        else
        {
            System.err.println("Connection failed. Restart Program.");
            System.exit(0);
        }

        //PrintWriter/BufferedReader instance/declaration to input/outputstream
        PrintWriter out = new PrintWriter(socket.getOutputStream());         
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        //User input statement
        Scanner input = new Scanner (System.in);
        System.out.print("Enter any greeting[s]: ");
        String greetingInput = input.nextLine();

        out.println(greetingInput); //Sends greetingInput to outputstream
        out.flush(); //Did not turn on autoflush

        System.out.println();

        //Extra Statement
        System.out.println("Sending Data...");

        //Checking if PrintWriter has error
        System.out.println("PrintWriter: " + out.checkError());

        //Checks if BufferedReader is ready.       
        System.err.println("BufferedReader: " + in.ready());
        System.out.println();

        //If BufferedReader is ready, code will compute whatever mainServer sends
        String serverResponse = null;
        while((serverResponse = in.readLine()) != null )
        {
            if(in.ready() == true)
            {
                 serverResponse += in.readLine();
                 System.out.println(serverResponse);
            }
            else
                System.err.println("Buffer not ready!");
        }
    }
}

服务器端:

import java.net.*;
import java.io.*;
import java.util.*;
public class mainServer
{
    public static void main (String [] args)throws IOException
    {
    ServerSocket echoServer = null;
    try
    {
        echoServer = new ServerSocket (13000);

            Socket socket = echoServer.accept();

            try
            {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                String clientInput = in.readLine();

                out.println("Client Input: " + clientInput);
                out.println("Server Responese: Salutations, today's a wonderfully frosty day.");
                out.flush();
            }
            catch(IOException e)
            {
                System.err.println("There was problem with BufferedReader/PrintWriter: " + e);
            }
            finally
            {
                socket.close();
            }
        }

        catch(IOException e)
        {
            System.err.println("Error with Sockets: " + e);
        }
        finally 
        {
            echoServer.close();
        }
    }
}

结果:

Client/Server Program Utilizing Sockets!

Connection: Working...

Enter any greeting[s]: Hi

Sending Data...
PrintWriter: false

BufferedReader: false
Buffer not ready!
Buffer not ready!
BUILD SUCCESSFUL (total time: 2 seconds)

2 个答案:

答案 0 :(得分:3)

大部分代码没有意义。

        Socket socket = new Socket(serverAddress, 13000);

        //Checks if connection is working
        System.out.print("Connection: ");
        if(socket.isConnected()== true)

此测试无法失败。如果套接字没有连接,则会抛出异常,您将无法访问此部分代码。

        //Checks if BufferedReader is ready.       

为什么?

        //If BufferedReader is ready, code will compute whatever mainServer sends
        String serverResponse = null;
        while((serverResponse = in.readLine()) != null )
        {
            if(in.ready() == true)
            {
                 serverResponse += in.readLine();
                 System.out.println(serverResponse);
            }
            else
                System.err.println("Buffer not ready!");
        }
    }

这都是胡说八道。进入循环后,已经serverResponse中有服务器响应的一行。然后,您正在检查更多输入,并将其下一行添加到serverResponse,如果没有更多输入,则表示您正在处理整个好像根本没有任何数据的情况。鉴于您的服务器发送了两行响应,您需要在循环内执行的操作是打印serverResponse并删除所有其余代码。 readLine()将阻塞,直到数据到达,流结束或抛出异常。

您的客户端还需要在退出之前关闭套接字。

NB == true!= false在遇到任何地方都是多余的。不要写这样的代码。

答案 1 :(得分:1)

@ EJP的回答是现货。我只想添加试图使用输入流和读者的ready()方法的代码通常以这种或那种方式被破坏。

在这种情况下,ready()调用没有实现任何有用的功能。最好只需调用readLine()并让它阻塞,直到有一条线可用。