【问题标题】:Exception in thread upon client disconnect客户端断开时线程中的异常
【发布时间】:2012-03-19 10:11:28
【问题描述】:

当客户端使用 CTRL-C 断开连接或连接终止时,服务器应该产生一条消息,说明它已经这样做了,而不是产生如下异常:

线程“Thread-1”中的异常 java.lang.NullPointerException 在 ChatServerThread.handleClient(ChatServerThread.java:31) 在 ChatServerThread.run(ChatServerThread.java:17)

在我分别从 read/writeUTF() 转换为 readLine() 和 writeByte() 并让服务器用客户端发送的内容响应客户端后,它开始表现出这种行为。详情请见Exception in thread

问题是如何让 EOFException 功能再次工作,以便 客户端关闭连接。 打印出异常消息。第 31 行是if( nextCommand.equals(".bye") ) {

import java.net.*;
import java.io.*;

//public class ChatServerThread implements Runnable
public class ChatServerThread extends Thread
{  private Socket          socket   = null;
   private ChatServer      server   = null;
   private int             ID       = -1;
   private BufferedReader streamIn =  null;
   private DataOutputStream streamOut = null;

   public ChatServerThread(ChatServer _server, Socket _socket)
   {  server = _server;  socket = _socket;  ID = socket.getPort();
   }
   public void run() {
   try {
       handleClient();
   } catch( EOFException eof ) { \\This does not seem to be working now and it previously was
        System.out.println("Client closed the connection.");
   } catch( IOException ioe ) {
        ioe.printStackTrace();
   }
}

   public void handleClient() throws IOException {
      boolean done = false;
      try {
      System.out.println("Server Thread " + ID + " running.");
      while (!done) {
        String nextCommand = streamIn.readLine();
        if( nextCommand.equals(".bye") ) {
           System.out.println("Client disconnected with bye.");
           done = true;
        } else {
           System.out.println( nextCommand );
           String nextReply = "You sent me: " + nextCommand.toUpperCase() + '\n';
           streamOut.writeBytes ( nextReply );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      streamOut = new DataOutputStream(socket.getOutputStream());
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }
}

【问题讨论】:

  • 第 31 行是哪一行?这与我对你上一个问题的反馈相同。请帮助我们。
  • 问题是如何让 EOFException 功能再次工作,以便 客户端关闭连接。 打印出异常消息。 31号线if( nextCommand.equals(".bye") ) {

标签: java multithreading exception exception-handling io


【解决方案1】:

异常从第二行抛出:

String nextCommand = streamIn.readLine();
if( nextCommand.equals(".bye") ) {

显然nextCommandnullstreamIn 是一个BufferedReader,引用readLine() 的JavaDoc:

返回:

包含行内容的字符串,不包括任何行终止字符,如果已到达流的末尾,则为 null

DataInputStream.readUTF() 相比,这是一种不同的行为:

返回:

Unicode 字符串。

投掷:

EOFException - 如果此输入流在读取所有字节之前到达末尾。

我的猜测是 Ctrl + C 会中断阻塞 readLine() 并发出信号流结束的信号。

【讨论】:

  • 所以readUTF 将读取 Ctrl+C 并允许} catch( EOFException eof ) {,如果是这种情况,有没有办法解决它?
  • 你得到了一个 NPE。你需要测试nextCommand==null
  • @Astron:我引用了 Javadoc 的相关部分,请参阅我的更新答案。
  • @JimGarrison 有例子吗?
  • @TomaszNurkiewicz 谢谢,我将确定恢复到readUTF() 是否合理或是否有修复。在这一点上,EOFException 似乎一文不值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
相关资源
最近更新 更多