0

I have the following code that needs to do a simple math SUM for 2 numbers.

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vlad Murea
  • 11
  • 3
  • Side issue here: `while (Integer.toString(num1) != "" && Integer.toString(num2) != "") {`. No good since 1) `==` should not be used to test for String equality but rather is used for *reference* equality. 2) Integer.toString(numAnything) will *never* be an empty String, so even the slightly more correct `Integer.toString(num1).isEmpty()` wouldn't make any sense at all here. – Hovercraft Full Of Eels May 02 '18 at 17:16
  • True, I realized that afterwards.. :) Thanks – Vlad Murea May 02 '18 at 19:53

1 Answers1

0

The problem is in your Server class. In integers num1 and num2 are not updated and thus the results is always the same. In order to change this, you have to put the readInt inside the while loop.

This Server class works:

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();    
    }
}
Molitoris
  • 935
  • 1
  • 9
  • 31