0

我有以下代码需要对 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();

}
}

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

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

4

1 回答 1

0

问题出在您的 Server 类中。在整数中num1并且num2不更新,因此结果总是相同的。为了改变这一点,你必须把readIntwhile 循环放在里面。

这个服务器类有效:

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();    
    }
}
于 2018-05-02T17:11:45.920 回答