带有Java套接字的客户端和服务器聊天应用程序。缓冲读取器readline()正在挂起

时间:2018-07-16 00:46:33

标签: java sockets networking server client

客户端类

public class ClientThread {
    private Socket socket;
    private BufferedReader br;
    private PrintWriter pw;

    public ClientThread() {
        try {
            socket = new Socket("127.0.0.1", 5000);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream());
        } catch (IOException e) {
            System.out.println("IOException in ClientThread constructor: " + e.getMessage());
        }
    }

    public void start() {
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("Enter your message");
            String message = sc.nextLine();
            pw.println(message);
            try {
                String responseFromServer;
                responseFromServer = br.readLine();
                // SUSPENDS ON LINE ABO
                System.out.println("Response from server: " + responseFromServer);
            } catch  (IOException e) {
                System.out.println("IOException in ClientThread start(): "  + e.getMessage());
            }

        }
    }
}

Client Main方法

public class Main {

    public static void main(String[] args) {
        new ClientThread().start();
    }
}

服务器类

public class ServerThread {

    private ArrayList<PrintWriter> outputStreams;

    public class ClientHandler implements Runnable {
        private Socket socket;
        private BufferedReader br;

        public ClientHandler(Socket socket) {
            this.socket = socket;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                System.out.println("IOException in SeverThread constructor: " + e.getMessage());
                e.printStackTrace();
            }
        }

        public void run() {
            String message;
            try {
                while((message = br.readLine()) != null) {
                // SUSPENDS ON LINE ABOVE
                    System.out.println("Read message from client: " + message);
                    tellEveryone(message);
                }
            } catch (IOException e) {
                System.out.println("IOException in ClientHandler run(): " + e.getMessage());
            }
        }
    }

    public void tellEveryone(String message) {
        for(PrintWriter pw : outputStreams) {
            pw.println(message);
            pw.flush();
        }
    }

    public void start() {
        outputStreams = new ArrayList<>();
        try {
            ServerSocket serverSocket = new ServerSocket(5000);
            while(true) {
                Socket socket = serverSocket.accept();
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                outputStreams.add(pw);
                Thread t = new Thread(new ClientHandler(socket));
                t.start();
                System.out.println("Got a connection");
            }
        } catch (IOException e) {
            System.out.println("IOException in ServerThread start(): " + e.getMessage());
        }
    }
}

Server Main方法

public class Main {


    public static void main(String[] args) {
        new ServerThread().start();
    }
}

每当我运行该程序时,客户端类和服务器类中的两个读取行方法似乎都将挂起,并且这两个类都无法通过套接字互相发送数据。这是什么原因呢?我相当喜欢新的Java网络。

0 个答案:

没有答案