即使没有启动异常,套接字也不会通信

Sockets not communicate even if no exception are launched

我正在尝试从客户端向服务器发送一个字符串,但即使所有代码编译都没有错误并且没有抛出异常,也没有发送任何内容。这是我的服务器:

        String nomeAccount = "";
        try {
            //PHASE 1: The server receives the email
            try {
                InputStream inStream = incoming.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                //if I print something here it works
                nomeAccount = in.readLine(); 
                //here nothing get printed
                System.out.println("Nome: "+nomeAccount);
            } catch (IOException ex) {
                System.out.println("Not works");
                Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    incoming.close();
                } catch (IOException ex) {
                    System.out.println("Not works");
                    Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

这是客户:

//PHASE 1: The client sends a string to the server
    try {
        InputStream inStream = s.getInputStream();
        OutputStream outStream = s.getOutputStream();
        PrintWriter out = new PrintWriter(outStream, true);
        out.write(account+"\n");

有几个问题是

  • PrintWriter 吃掉任何异常抛出。如果它们被抛出,您将看不到它们。
  • 使用 PrintWriter 而不是普通 Writer 的目的是使用 printlnprintprintf
  • 除非你打印一个新行,否则它不会刷新数据(它应该与 write() 一起工作,但 println 更清晰)