为什么客户端在成功连接到服务器后会冻结?

时间:2018-06-13 23:04:25

标签: java networking server client

我正在尝试使用javaSocketServerSocket中创建聊天室,并且客户端和服务器都通过IP地址成功连接到彼此。但是一旦我连接并尝试在textField中输入内容,整个聊天室就会冻结,我必须退出应用程序才能停止它。你知道为什么会这样吗?

这是服务器

public class Server {

public static void main(String[] args) throws IOException {
    System.out.println("The server is running");
    int clientNumber = 1; 
    ServerSocket listener = new ServerSocket(9092); 
    try{
        while(true){
            new Chat(listener.accept(), clientNumber++).start();
        }
    }finally{
        listener.close(); 
    }
}

private static class Chat extends Thread{
    private Socket socket; 
    private int clientNumber; 

    public Chat(Socket socket, int clientNumber) {
        this.socket = socket; 
        this.clientNumber = clientNumber; 
        System.out.println("You have connected as " + clientNumber +".");
    }

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

            out.println("You can chat with any other people who have connected");
            out.println("To leave just enter a single slash"); 
            out.println(clientNumber + " people have connected.");

            while(true){
                String input = in.readLine(); 
                if(input == null || input.equals("/")){
                    break; 
                }
                out.println("Client #" + clientNumber +":" + input);
            }
        }catch (IOException e) {
            System.out.println("Error handling client# " + clientNumber + ": " + e);
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                System.out.println("Couldn't close socket");
            }
            System.out.println("Client #" + clientNumber + " has ended the connection");

        }

    }

}

}

这是客户

public class Client implements KeyListener {

private BufferedReader in; 

private PrintWriter out;

private JFrame frame = new JFrame("Chat Room"); 

private JTextField dataField = new JTextField(100); 

private JTextArea messageArea = new JTextArea(10, 100); 



public Client(){

    messageArea.setEditable(false);

    frame.getContentPane().add(dataField, "South");
    frame.getContentPane().add(messageArea, "Center");

    dataField.addKeyListener(this);

}



public void connectToServer() throws Exception{

    String serverAddress = JOptionPane.showInputDialog(frame,

            "Enter IP Address of the Server:",

            "Welcome to the Capitalization Program",

            JOptionPane.QUESTION_MESSAGE);



    Socket socket = new Socket(serverAddress, 9092); 

    in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

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



    for(int i =0; i<3; i++){

        messageArea.append(in.readLine() +"\n");

    }

}



public static void main(String[] args) throws Exception {

     Client client = new Client(); 

     client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     client.frame.pack(); 

     client.frame.setVisible(true);
     client.connectToServer();

}



@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}



@Override
public void keyPressed(KeyEvent e) {
    int c = e.getKeyCode(); 
    if(c == KeyEvent.VK_ENTER) {

        out.println(dataField.getText());

        String response; 

        try{

            response = in.readLine(); 

            if(response == null || response.equals("")){

                System.exit(0);

            }

        } catch (IOException ex){

            response = "Error: " + ex; 

        }

        messageArea.append(response + "\n");

        dataField.setText("");


    }

}



@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

}

0 个答案:

没有答案