计算器AWT - 总和2数字 - GUI接口 - 服务器/客户端

时间:2018-05-02 16:49:02

标签: java tcp awt socket.io-java-client

我有以下代码需要对2个数字进行简单的数学运算。

服务器代码:

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);
    }
}
}

客户代码:

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();

}
}

当我第一次运行代码时,我得到了结果,但是当我尝试输入不同的数字来计算(或相同的数字)时,我的计算器陷入了循环。

有人可以解释这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

问题出在您的Server类中。在整数中,num1num2未更新,因此结果始终相同。要更改此设置,您必须将readInt放在while循环中。

此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();    
    }
}