简体   繁体   English

计算器AWT-总计2个数字-GUI界面-服务器/客户端

[英]Calculator AWT - Sum 2 numbers - GUI Interface - Server/Client

I have the following code that needs to do a simple math SUM for 2 numbers. 我有以下代码需要对2个数字进行简单的数学求和。

Server Code: 服务器代码:

public class Server {

private ServerSocket server;
private Socket connection;

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

    try (
            ServerSocket server = new ServerSocket(8080);
            Socket connection = server.accept();

            DataInputStream dis = new DataInputStream(connection.getInputStream());
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());) {
        System.out.print("Connection Successful with the Calculator \n");
        //Get numbers

        int num1 = dis.readInt();
        int num2 = dis.readInt();
        while (Integer.toString(num1) != "" && Integer.toString(num2) != "") {
            int answer = num1 + num2;

            //Send Results
            dos.writeInt(answer);
        }
    } catch (IOException ie) {
        System.out.println(ie);
    }
}
}

Client Code: 客户代码:

public class Client {

private int num1;
private int num2;
private int result;

/**
 * @throws java.io.IOException
 */
public Client() throws IOException {
    try {

        //Conectam aplicatia la Server
        Socket server = new Socket("127.0.0.1", 8080);
        DataInputStream dis = new DataInputStream(server.getInputStream());
        DataOutputStream dos = new DataOutputStream(server.getOutputStream());

        //Interfata GUI
        Frame f = new Frame();
        f.setLayout(null);
        f.setTitle("Calculator");
        f.setBounds(100, 100, 435, 300);

        Label calc = new Label("Calculator v0.2");
        calc.setBounds(149, 48, 109, 24);
        calc.setFont(new Font("Arial", Font.PLAIN, 14));
        calc.setAlignment(Label.CENTER);

        TextField numar1 = new TextField();
        numar1.setBounds(36, 106, 83, 22);

        Label label_1 = new Label("+");
        label_1.setFont(new Font("Dialog", Font.PLAIN, 16));
        label_1.setBounds(139, 106, 13, 22);

        TextField numar2 = new TextField();
        numar2.setBounds(175, 106, 83, 22);

        Label egal = new Label("=");
        egal.setFont(new Font("Dialog", Font.PLAIN, 16));
        egal.setBounds(264, 106, 13, 22);

        TextField rezultat = new TextField();
        rezultat.setBounds(283, 106, 109, 22);
        rezultat.setEditable(false);
        rezultat.setText("");

        //        Button reset = new Button("Reset");
        //   reset.setBounds(175, 185, 83, 22);
        //    reset.addActionListener(new ActionListener() {
        //          @Override
        //          public void actionPerformed(ActionEvent e) {
        //              rezultat.setText("");
        //              numar1.setText("");
        //              numar2.setText("");
        //          }
        //       });
        Button calcul = new Button("Calculeaza");
        calcul.setBackground(SystemColor.activeCaption);
        calcul.setForeground(SystemColor.textText);
        calcul.setBounds(0, 227, 434, 34);

        calcul.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int num1 = Integer.parseInt(numar1.getText());
                int num2 = Integer.parseInt(numar2.getText());
                //Send Values

                try {

                    dos.writeInt(num1);
                    dos.writeInt(num2);

                    //Get Result
                    int answer;
                    answer = dis.readInt();

                    //Set value to "Result text field"
                    rezultat.setText(Integer.toString(answer));

                } catch (IOException ex) {
                   System.out.print(ex);
                }

            }

        });

        f.add(calc);
        f.add(numar1);
        f.add(label_1);
        f.add(numar2);
        f.add(egal);
        //f.add(reset);
        f.add(rezultat);
        f.add(calcul);

        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {

                System.exit(0);

            }

        });

    } catch (HeadlessException | IOException ex) {
    }

}

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

    Client cl = new Client();

}
}

When I run the code for the first time, I get the result, but when I try to enter different numbers to calculate (or the same numbers) my calculator gets stuck in a loop. 第一次运行代码时,会得到结果,但是当我尝试输入不同的数字进行计算(或相同的数字)时,我的计算器陷入了循环。

Can someone explain what is wrong here? 有人可以解释这里有什么问题吗?

The problem is in your Server class. 问题出在您的Server类中。 In integers num1 and num2 are not updated and thus the results is always the same. 在整数num1num2中不会更新,因此结果始终相同。 In order to change this, you have to put the readInt inside the while loop. 为了更改此设置,必须将readInt放入while循环中。

This Server class works: 该Server类适用于:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    private ServerSocket server;
    private Socket connection;

    Server() {

        try {
            ServerSocket server = new ServerSocket(8080);
            Socket connection = server.accept();

            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            DataInputStream dis = new DataInputStream(connection.getInputStream());

            while (true) {
                int num1 = dis.readInt();
                int num2 = dis.readInt();

                dos.writeInt(num1 + num2);

            }
        } catch (IOException e) {
        }
    }

    public static void main(String[] args) {

        Server s = new Server();    
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM