Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缓冲区侦听器到服务器输出错误_Java_Bufferedreader - Fatal编程技术网

Java 缓冲区侦听器到服务器输出错误

Java 缓冲区侦听器到服务器输出错误,java,bufferedreader,Java,Bufferedreader,我在侦听服务器输出时遇到问题。我想能够打印一些东西到控制台一旦其中一个引用的命令。我面临的问题是,它似乎是在听,但却不配合节目的其余部分。因此,当我尝试单击GUI上的按钮时,它会卡住 public class MainFrame implements Runnable { //declare Jpanel private static JFrame frmHome; // The client socket private static Socket clien

我在侦听服务器输出时遇到问题。我想能够打印一些东西到控制台一旦其中一个引用的命令。我面临的问题是,它似乎是在听,但却不配合节目的其余部分。因此,当我尝试单击GUI上的按钮时,它会卡住

public class MainFrame implements Runnable {
    //declare Jpanel
    private static JFrame frmHome;

    // The client socket
    private static Socket clientSocket = null;
    // The output stream
    static ObjectOutputStream os;
    // The input stream
    static ObjectInputStream is;
    private static BufferedReader inputLine = null;
    private static boolean closed = false;

    public static void main(String[] args) throws IOException{
        // The default port.
        int portNumber = 3333;
        // The default host.
        String host = "localhost";
        /*
         * Open a socket on a given host and port. Open input and output streams.
         */
        try {
          clientSocket = new Socket(host, portNumber);
          is = new ObjectInputStream(clientSocket.getInputStream());
          os = new ObjectOutputStream(clientSocket.getOutputStream());
          os.flush();
          System.out
          .println("CONNECTED TO SERVER\n"
              + "Now using host=" + host + ", portNumber=" + portNumber);
        } catch (UnknownHostException e) {
          System.err.println("Don't know about host " + host);
        } catch (IOException e) {
          System.err.println("Couldn't get I/O for the connection to the host "
              + host);
        }

        /*
         * If everything has been initialized then we want to write some data to the
         * socket we have opened a connection to on the port portNumber.
         */
        if (clientSocket != null && os != null && is != null) {
          /* Create a thread to read from the server. */
          new Thread(new MainFrame()).start();
          os.writeObject("Home");
          os.flush();
        }
      }

      public void run() {
        /*
         * Keep on reading from the socket till we receive "Bye" from the
         * server. Once we received that then we want to break.
         */
          MainFrame window = new MainFrame();
          MainFrame.frmHome.setVisible(true);
        String responseLine;
        try {
          while (!closed) {
这里是我尝试创建缓冲区读取器的地方。在GUI的其他部分成功执行任务之后,会出现两个命令“Modify,OK”和“AdStudent OK”

              BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String line;
              try {
                while((line = br.readLine()) != null){
                    if (br.readLine().contains("Modify,OK") 
                            || br.readLine().contains("AddSudent ok")){
                        System.out.println("did it");
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
          }
        } finally{
            try {
                is.close();
                os.close();
                clientSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        }

    public MainFrame() {
        initialize();
    }

    //function to make window visible
    void setVisible() throws IOException {
        main(null);
    }

    private void initialize() {
        //Initialise Main window with 3 options.
        frmHome = new JFrame();
        frmHome.setTitle("Home");
        frmHome.setBounds(100, 100, 300, 372);
        frmHome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHome.getContentPane().setLayout(null);
        frmHome.setResizable(false);

        JLabel lblWelcomeToSrs = new JLabel("Welcome to SRS");
        lblWelcomeToSrs.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblWelcomeToSrs.setBounds(86, 183, 112, 14);
        frmHome.getContentPane().add(lblWelcomeToSrs);

        //initialise all buttons and labels of window.
        JButton btnAdStu = new JButton("Add a student");
        btnAdStu.setBounds(10, 207, 126, 23);
        btnAdStu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AddStudentFrame adus;
                try {
                    try {
                        adus = new AddStudentFrame();
                        adus.setVisible();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    frmHome.setVisible(false);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        });
        frmHome.getContentPane().add(btnAdStu);

        JButton btnCheckStud = new JButton("Search / Modify");
        btnCheckStud.setBounds(146, 207, 127, 23);
        btnCheckStud.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SearchFrame searchFrame;
                searchFrame = new SearchFrame();
                searchFrame.setVisible();
            }
        });
        frmHome.getContentPane().add(btnCheckStud);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(0, 0, 0, 0);
        frmHome.getContentPane().add(lblNewLabel);

        JLabel lblCreatedByRmi = new JLabel("Created by R\u00E9mi Tuyaerts");
        lblCreatedByRmi.setBounds(147, 318, 184, 14);
        frmHome.getContentPane().add(lblCreatedByRmi);  

        JButton btnNewButton = new JButton("Complete List of Students");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                CompleteListFrame studentList = new CompleteListFrame();
                studentList.setVisible();
            }
        });
        btnNewButton.setBounds(52, 241, 184, 23);
        frmHome.getContentPane().add(btnNewButton);

        // wonderful pictures of his excellence design by Yasser
        JLabel lblNewLabel_1 = new JLabel("");
        Image img = new ImageIcon(frmHome.getClass().getResource("/michaelchung.jpg")).getImage();
        lblNewLabel_1.setIcon(new ImageIcon(img));
        lblNewLabel_1.setBounds(80, 11, 120, 148);
        frmHome.getContentPane().add(lblNewLabel_1);
    }
}

我想这里有些不好的地方是
if(br.readLine().contains(“Modify,OK”)| | br.readLine().contains(“AddSudent OK”){System.out.println(“did”)}
当你说
br.readLine()
它将转到下一行,我认为您应该使用
line.contains(“您的命令”)
hmmm不,它似乎不起作用对不起:/console中的错误是什么…?]没有错误,它只是被卡住了,就像在一个无限循环中一样