java-serverside for(;;) 当 readline() 为 null 时循环停止

标签 java server-side bufferedreader

我对我的服务器端代码有一些疑问。由于我不知道如何调试服务器端代码,我在这里询问可能是什么原因。在我的 for(;;) 循环中,如果消息出现 null,则 for 循环会在进入我的 if(msg ==null) 语句,所以我无法广播它可能是什么问题?谢谢

public class ChatServer { 
    String gonderen1;
    String alan1;

    private static final int PORT_NUMBER = 8000;
    Map<String, List<PrintWriter>> clients;
    List<PrintWriter> clientwriter;
    ArrayList<String> liste = new ArrayList<String>();

    /** Creates a new server. */
    public ChatServer() {
        clients = new HashMap<String, List<PrintWriter>>();
        clientwriter = new LinkedList<PrintWriter>();
    }

    /** Starts the server. */
    public void start() {
        try {
            ServerSocket s = new ServerSocket(PORT_NUMBER); 
            for (;;) {
                Socket incoming = s.accept(); 
                new ClientHandler(incoming).start(); 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Adds a new client identified by the given print writer. */
    private void addClient(PrintWriter out,String alan) {
        synchronized(clientwriter) {    
            clientwriter.add(out);
            clients.put(alan, clientwriter);    
        }
    }

    /** Adds the client with given print writer. */
    private void removeClient(PrintWriter out) {
        synchronized(clientwriter) {    
            clientwriter.remove(out);
            clients.remove(alan1);  
        }
    }

    private void broadcast(String msg,String alan) {
        for(PrintWriter out : clients.get(alan)){
            out.println(msg);
            out.flush();
        }
    }

    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println(USAGE);
            System.exit(-1);
        }
        new ChatServer().start();
    }

    private class ClientHandler extends Thread {

        private Socket incoming; 

        public ClientHandler(Socket incoming) {
            this.incoming = incoming;
        }

        public void run() {
            PrintWriter out = null;
            try {
                out = new PrintWriter(
                        new OutputStreamWriter(incoming.getOutputStream()));

                // inform the server of this new client           
                /* ChatServer.this.addClient(out,"mert");
                out.print("Welcome to AndyChat! mert");
                out.println("Enter BYE to exit."); 
                out.flush();*/

                BufferedReader in 
                    = new BufferedReader(
                        new InputStreamReader(incoming.getInputStream())); 
                String msg;
               for(;;)  {
                   msg = in.readLine();
                   if(msg!=null){
                       liste.add(msg);
                       Pattern pat = Pattern.compile("<Gonderen>(.*?)</Gonderen>");
                       Matcher mat = pat.matcher(msg);
                       if (mat.find()) {
                           gonderen1 = mat.group(1).replaceAll("\\s",""); // => "3"
                           System.out.println("Gonderen "+gonderen1);
                       }
                       Pattern p = Pattern.compile("<Alan>(.*?)</Alan>");
                       Matcher m = p.matcher(msg);

                       if (m.find()) {
                           alan1 = m.group(1).replaceAll("\\s",""); // => "3"           
                           out.print("dsa");
                           System.out.println("Alanin adi "+alan1);
                       }
                       if(alan1!=null){
                           System.out.println(clients.get("mert"));
                           ChatServer.this.addClient(out,alan1);
                       }
                   } else {                         
                       for(int i =0;i<liste.size();i++){
                       ChatServer.this.broadcast(msg,"mert");
                       System.out.println("null gelmedi");}
                       break;
                   }

               }

               incoming.close(); 
               ChatServer.this.removeClient(out);
            } catch (Exception e) { 
                if (out != null) {
                    ChatServer.this.removeClient(out);
                }
                e.printStackTrace(); 
            }
        }
    }
}

最佳答案

Matcher mat = pat.matcher(msg); 如果您向其传递 null 消息,可能会给出 NullPointerException

您应该将 if(msg==null) 检查移至 Matcher mat = pat.matcher(msg); 之前。

pat.matcher(msg)
  calls new Matcher (pat, msg)
  which assigns this.text = msg (which is null) and then calls reset()
  which calls to = getTextLength()
  which returns text.length() // which is where the NullPointerException will be
                              // thrown

关于java-serverside for(;;) 当 readline() 为 null 时循环停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26201221/

相关文章:

javascript - 在 AngularJS 1.4 中使用服务器端 JSON 数组对象作为登录详细信息

timer - 以秒级精度为用户任务计时

jquery - 初始化服务器端处理数据表后如何调整列?

java - 在文件上使用 BufferedReader 时出现 FileNotFoundException

java - BufferedReader 的安全实现

java - API 请求多次使用相同的截击类?

java - Java中是否存在不受支持的操作注释?

java - 递归地将集合扫描到 map 中

java - 如何解析嵌套在 json 数组中的 json 对象

Java堆空间不足: after -XmX1G