readLine()抛出聊天程序中的异常,我无法弄清楚原因

时间:2016-04-07 18:00:03

标签: java sockets bufferedreader

我写了一个小聊天程序,共有3个类:TalkServer(服务器),TalkDienst(管理客户端之间数据交换的线程类)和ChatProgram(客户端)。

TalkDienst:

    public class TalkDienst extends Thread {
    private Socket send, rec;
    private BufferedReader in;
    private PrintWriter out;
    private boolean sendClosed, recClosed;

    public TalkDienst(Socket send, Socket rec) {
        this.send = send;
        this.rec = rec;

        try {
            this.in = new BufferedReader(new InputStreamReader(this.rec.getInputStream()));
            this.out = new PrintWriter(new OutputStreamWriter(this.send.getOutputStream()), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.start();
    }

    @Override
    public void run() {
        // this.out.println("--- Partner found ---");
        System.out.println("Partner found");
        while (true) {
            try {
                String input = this.in.readLine();
                System.out.println("test");
                if (!input.equals(null)) {
                    this.out.println(input);
                }
            } catch (IOException e) {
                closeall();
                e.printStackTrace();
                break;
            } catch (NullPointerException e) {
                closeall();
                e.printStackTrace();
                break;
            }
        }
    }

    private void closeall() {
        try {
            this.send.close();
            this.rec.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ChatProgram:

    public class ChatProgramm extends JFrame implements Runnable
{
    private Thread t;
    private Socket s;
    private Container c;
    private JTextArea ta;
    private JScrollPane sp;
    private JScrollBar sb;
    private JTextField tf;

    private BufferedReader in;
    private PrintWriter out;

    private boolean socketClosed;

    public ChatProgramm(Socket h)
    {
        this.t = new Thread(this);

        this.s = h;
        try 
        {
            this.in = new BufferedReader(new InputStreamReader(this.s.getInputStream()));
            this.out = new PrintWriter(new OutputStreamWriter(this.s.getOutputStream()), true);
        } catch (IOException e) 
        {
            e.printStackTrace();
        }
        this.t.start();
//Just some UI things...[...]

        this.ta = new JTextArea();
        this.ta.setEditable(false);
        this.ta.setText("--- Waiting for partner ---");

        this.tf = new JTextField();
        this.tf.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                if(tf.getText().length() > 0)
                {
                    out.println(">>>: " + tf.getText());
                    ta.setText(ta.getText() + "\n<<<: " + tf.getText());
                    tf.setText("");
                }
            }

        });

        this.c.add(this.sp);
        this.c.add(this.tf);

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                String input = this.in.readLine();
                System.out.println("warten");
                if(!input.equals(null))
                {
                    this.ta.setText(this.ta.getText() + "\n" + input);
                    this.sb.validate();
                    this.sb.setValue(this.sb.getMaximum());
                }
            } catch (IOException e)
            {
                closeall();
                e.printStackTrace();
                break;
            } catch (NullPointerException e)
            {
                closeall();
                e.printStackTrace();
                break;
            }
        }
    }

    private void closeall()
    {
        try
        {
            this.s.close();
            this.ta.setText(this.ta.getText() + "\n--- Connection lost ---");
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

当我删除

上的评论时
  

// this.out.println(“---找到合作伙伴---”);

在TalkDienst中,该程序将无法运行并将给我这些错误消息:左侧窗口显示TalkDienst,右上方是客户端1和右下方客户端2

Error messages and clients

以下是文本格式的请求错误消息: TalkServer:

java.net.SocketException: Socket closed
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at TalkDienst.run(TalkDienst.java:40)
java.lang.NullPointerException
        at TalkDienst.run(TalkDienst.java:42)

ChatProgramm 1 :(先开始)

java.lang.NullPointerException
        at ChatProgramm.run(ChatProgramm.java:131)
        at java.lang.Thread.run(Unknown Source)

ChatProgramm 2:

Exception in thread "Thread-1" java.lang.NullPointerException
        at ChatProgramm.closeall(ChatProgramm.java:156)
        at ChatProgramm.run(ChatProgramm.java:144)
        at java.lang.Thread.run(Unknown Source)

感谢您的帮助,提前谢谢!

1 个答案:

答案 0 :(得分:0)

println()不会抛出异常,包括readLine()抛出的异常,因为您关闭了套接字并继续使用它。

您的流检查结束也是错误的。惯常检查是:

while ((line = in.readLine()) != null
    // ...