套接字输出流的 print() 方法不起作用

问题描述 投票:0回答:1

我正在尝试使用 java 套接字为一个简单的聊天应用程序编写代码。这是服务器的代码


public class MyServerSocket implements Runnable {

    List<Connexion> connexions = new ArrayList<>();
    final int PORT=7878;

    @Override
    public  void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
            System.out.println ("I'm waiting for clients to connect");
            Socket socket = null;
            while (true) {
                socket = serverSocket.accept();
                System.out.println("A client is connected, IP : " + socket.getInetAddress());
                Connexion connexion = new Connexion(socket);
                connexions.add(connexion);
                Thread process_connexion = new Thread(connexion);
                process_connexion.start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // serverSocket.close();
    }

    public void broadcast(String message) {
        this.connexions.forEach(connexion -> {
            if (connexion != null) {
                connexion.sendMessage(message);
            }
        });
    }

    class Connexion implements Runnable{

        private Socket socket = null;
        private String nickName;
        private BufferedReader in;
        private PrintWriter out;

        public Connexion (Socket s) {
            this.socket = s;
        }

        public void run() {
            try {
                InputStream IS = socket.getInputStream();
                InputStreamReader ISR = new InputStreamReader(IS);
                in = new BufferedReader(ISR);
                OutputStream OS = socket.getOutputStream();
                out = new PrintWriter(OS, true);



                // here is the problem
                // when i replace println with print method
                // the code won't work any more
                out.println("Hi Client");
                out.println("Enter your name : ");





                String requete = in.readLine();
                this.nickName = requete;
                out.println("You are connected, you can type what you want!");
                while ((requete = in.readLine()) != null) {
                    broadcast(this.nickName + " : " + requete);
                    System.out.println(requete);
                }
                out.println("Connection lost!");
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                try {
                    socket.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }

        public void sendMessage(String message) {
            out.println(message);
        }

    }

}

代码正在工作,但是当我替换提到的 println 方法(上面的代码)或任何与套接字输出流相关的 println 方法时,会出现打印问题。

当我使用 println 时,服务器的输出看起来像 output of the server 客户端输出看起来像 client output 但是当我使用 print 代替时,服务器的输出看起来像 output of the server 客户端输出看起来像 client output 我在两个示例中使用相同的客户端代码。 我的问题是为什么 print 方法不像 println 那样工作。

java sockets outputstream
1个回答
0
投票

来自 Javadoc

与 PrintStream 类不同,如果启用了自动刷新,则仅当调用

println
printf
format
方法之一时才会执行自动刷新,而不是在恰好输出换行符时执行。这些方法使用平台自己的行分隔符概念而不是换行符。

由于您不再调用

println
,因此不会自动冲洗。手动调用
flush()
发送数据。

© www.soinside.com 2019 - 2024. All rights reserved.