Java无法同时读取和写入套接字

时间:2017-06-02 18:06:51

标签: java multithreading sockets

我正在尝试使用java套接字构建一个简单的多客户端聊天应用程序。我这样做的方法是让一个客户端类连接到等待客户端连接的服务器类,并创建一个新线程来处理该客户端(读取和写入套接字连接的地方)。客户端还读取和写入此线程的套接字连接。但是,当客户端写入套接字的输出流时,服务器将不响应。这里发布了一个类似的问题:

Can you write to a sockets input and output stream at the same time?

这里的答案之一是,只要从套接字读取在一个单独的线程上完成,就可以同时读取和写入套接字。

这是我的客户端应用程序:

    public class Client {

    Socket socket;

    public static void main(String[] args) {
        new Client();
    }

    public Client() {
        try {
            socket = new Socket("localhost", 4444);

            new Thread() {

                @Override
                public void run() { //read from the input stream

                    try(
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    ) {
                        String line;
                        while( (line = in.readLine()) != null ) {
                            System.out.println("Server said: " + line);
                        }
                    } catch(IOException e) {

                    }

                }

            }.start();

            //write to output stream
            try(
                PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                Scanner userInput = new Scanner(System.in);
            ){
                System.out.println("Enter Something: ");
                if(userInput.hasNextLine()) {
                    out.println(userInput.nextLine());
                }
            } catch (IOException e) {

            }

        } catch(IOException e) {

        }


    }
}

我的服务器应用程序:

public class Server {
    ServerSocket ss;


    public static void main(String[] args) {
        new Server();
    }

    public Server() {
        System.out.println("Server Running...");
        try {
            ss = new ServerSocket(4444);

            while(true) {


                Socket socket = ss.accept();
                new Thread() { //create new thread connection to client
                    @Override
                    public void run() {

                        new Thread() { //thread that reads inputstream
                            @Override
                            public void run() {

                                try(
                                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                                ) {
                                    String line;
                                    while( (line = in.readLine()) != null ) {
                                        System.out.println("Client said: " + line);
                                        //The problem seems to lie here.
                                    }
                                } catch(IOException e) {

                                }

                            }
                        }.start();

                        //write to outputstream
                        try (
                            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                        ) {
                            String sendToClient = "Hey, my name is Server007 B)";

                            out.println(sendToClient);

                        } catch(IOException e) {

                        }
                    }
                }.start();

            }


        } catch (IOException e) {}
    }
}

我将运行服务器,然后运行客户端,在客户端输出

Server said: Hey, my name is Server007
Enter something: 
Hello! <- enter anything

但是服务器没有打印&#39;客户说:你好!&#39;就像我期望的那样。我希望我的问题足够清楚,谢谢。

1 个答案:

答案 0 :(得分:1)

好的,所以我想出来了,如果有人犯同样的错误,我会回答我自己的问题。 PrintWriter构造函数应该是这样的:

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

不是这个:

PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

或者,我本可以这样做:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

我一定是在BufferedWriter和PrintWriter之间感到困惑:P