【问题标题】:Client Server Java problem with WindowBuilderWindowBuilder 的客户端服务器 Java 问题
【发布时间】:2020-11-11 21:06:56
【问题描述】:

我编写代码试图实现客户端服务器聊天。 我创建了两个 JFrame(客户端和服务器) 他们可以发送和接收消息,直到其中一个写“退出”,但在这种情况下存在问题。

客户端:

public class Client extends JFrame{

    private JPanel contentPane;
    private JTextField testo;
    static JTextArea area;
    static Socket client;
    static DataInputStream dis;
    static DataOutputStream dos;

    public static void main(String[] args) throws UnknownHostException, IOException {
        client=new Socket("127.0.0.1",1234);
        dis=new DataInputStream(client.getInputStream());
        dos=new DataOutputStream(client.getOutputStream());
                try {
                    Client frame = new Client();
                    frame.setVisible(true);
                    String msg="";
                    while(msg!="exit") {
                     msg=dis.readUTF();
                    System.out.println(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            
    }

    /**
     * Create the frame.
     */
    public Client(){
        System.out.println("Metodo Client-ingresso");
        setTitle("Client");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 599, 401);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        testo = new JTextField();
        testo.setBounds(10, 315, 421, 19);
        contentPane.add(testo);
        testo.setColumns(10);
        
        area = new JTextArea();
        area.setEditable(false);
        area.setBounds(10, 10, 565, 295);
        contentPane.add(area);
        
        JButton bottone = new JButton("Invia");
        bottone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                area.append(testo.getText());
                try {
                    String invia=testo.getText();
                    dos.writeUTF(invia);
                    if(invia=="Exit") {
                        client.close();
                        System.exit(0);
                    }
                } catch (IOException e1) {
                    System.out.println("Client errore in Invio sul DOS");
                }
            }
        });
        bottone.setBounds(441, 315, 134, 21);
        contentPane.add(bottone);
        System.out.println("Uscita dal metodo client");
    }

    }

服务器端:

    public class Server extends JFrame{

    private JPanel contentPane;
    private JTextField testo;
    static JTextArea area;
    static ServerSocket ss;
    static Socket server;
    static DataInputStream dis;
    static DataOutputStream dos;
    /**
     * Launch the application.
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        ss=new ServerSocket(1234);
        server=ss.accept();
        dis=new DataInputStream(server.getInputStream());
        dos=new DataOutputStream(server.getOutputStream());
                try {
                    Server frame = new Server();
                    frame.setVisible(true);
                    String msg="";
                    while(msg!="exit") {
                     System.out.println("Il Server è qui");
                     msg=dis.readUTF();
                    System.out.println(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
    }

    /**
     * Create the frame.
     */
    
    public Server() {
        setTitle("Server");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 419);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        testo = new JTextField();
        testo.setBounds(10, 329, 444, 19);
        contentPane.add(testo);
        testo.setColumns(10);
        
        area = new JTextArea();
        area.setEditable(false);
        area.setBounds(10, 10, 566, 308);
        contentPane.add(area);
        
        JButton bottone = new JButton("Invia");
        bottone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                area.append(testo.getText());
                try {
                    String invia=testo.getText();
                    dos.writeUTF(invia);
                    if(invia=="exit") {
                        ss.close();
                        server.close();
                        System.exit(0);
                    }
                } catch (IOException e1) {
                    System.out.println("S errore in invio sul DOS.");
                }
            }
        });
        bottone.setBounds(464, 328, 112, 21);
        contentPane.add(bottone);
    }

}

问题出在这里:

    while(msg!="exit") {
        msg=dis.readUTF();
                }

如果我写退出,while 循环永远不会结束,我需要使用蛮力关闭,所以由于 dis.readUTF 上的异常而出现错误。

谁能帮帮我?

【问题讨论】:

    标签: user-interface server client windowbuilder


    【解决方案1】:

    我试过这个解决方案,但问题是一样的:

       String mes="";
        while(!(mes=dis.readUTF()).equals("exit") && (mes=dis.readUTF())!=null) {  
                area.append("S:"+mes);
        }
    

    我不明白为什么!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 2016-11-07
      • 2011-05-26
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多