基本I / O程序

时间:2017-11-01 18:15:59

标签: java io

我正在尝试用Java学习一些I / O,而且我已经在这个问题上坚持了一段时间......在执行我的服务器端之后,我执行了我的客户端。问题是,在输入两个数字后,它不会从服务器返回值 - 它只是运行循环并让我继续输入值。我不知道如何调试并找出哪些连接无效。

public class Client {

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

        Socket soc = new Socket("localhost", 9806);

        BufferedReader userInput = new BufferedReader(new InputStreamReader((System.in)));

        BufferedReader in = new BufferedReader(new InputStreamReader((soc.getInputStream())));

        PrintWriter out = new PrintWriter(soc.getOutputStream(), true);

        int option = 0;
        int num1 = 0;
        int num2 = 0;

        do {
            System.out.println("Choose an operation");
            System.out.println("1 Addion");
            System.out.println("2 Subtraction");
            System.out.println("3 Multiplication");
            System.out.println("4 Division");
            System.out.println("Enter an option");
            option = Integer.parseInt(userInput.readLine());

            System.out.println("Enter number");
            num1 = Integer.parseInt(userInput.readLine());

            System.out.println("Enter number");
            num2 = Integer.parseInt(userInput.readLine());

            out.println(option + ":" + num1 + ":" + num2);

            String answer = in.readLine();
            System.out.println("Server says: " + answer);
        } while(true);
    }
}

服务器端:

public class Server {

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

        ServerSocket ss = new ServerSocket(9806);
        System.out.println("Waiting for client");

        Socket soc = ss.accept();

        System.out.println("Connection established");

        BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

        PrintWriter out = new PrintWriter(soc.getOutputStream(), true);

        while(true){
            String str[] = in.readLine().split(":");

            int option = Integer.parseInt(str[0]);
            int num1 = Integer.parseInt(str[1]);
            int num2 = Integer.parseInt(str[2]);
            String result = "";

            switch(option) {
                case 1: result = "Addition is: " + (num1 + num2);break;
                case 2: result = "Subtraction is: " + (num1 - num2);break;
                case 3: result = "Multiplication is: " + (num1 * num2);break;
                case 4: result = "Division is: " + (num1 / num2);break;
                default: break;
            }
            out.print(result);

        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有在服务器中使用println来实现`autoFlush,

并且您在输入流

中滥用了while

并尝试使用try-with-resource来利用AutoCloseable界面。

请参阅此代码:

服务器类

public class Server {

    public static void main(String[] args) {
        System.out.println("Waiting for client");

        try (ServerSocket serverSocket = new ServerSocket(9806);
                Socket socket = serverSocket.accept();
                BufferedReader socketInputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter socketOutputWriter = new PrintWriter(socket.getOutputStream(), true);) {

            System.out.println("Connection established");

            String line;

            while ((line = socketInputReader.readLine()) != null) {
                String str[] = line.split(":");

                int operation = Integer.parseInt(str[0]);
                int num1 = Integer.parseInt(str[1]);
                int num2 = Integer.parseInt(str[2]);
                String result = "";

                switch (operation) {
                case 1:
                    result = "Addition is: " + (num1 + num2);
                    break;
                case 2:
                    result = "Subtraction is: " + (num1 - num2);
                    break;
                case 3:
                    result = "Multiplication is: " + (num1 * num2);
                    break;
                case 4:
                    result = "Division is: " + (num1 / num2);
                    break;
                default:
                    break;
                }
                socketOutputWriter.println(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端类

public class Client {

    public static void main(String[] args) {

        try (Socket socket = new Socket("localhost", 9806);
                BufferedReader userInput = new BufferedReader(new InputStreamReader((System.in)));
                BufferedReader socketInputReader = new BufferedReader(new InputStreamReader((socket.getInputStream())));
                PrintWriter socketOutputWriter = new PrintWriter(socket.getOutputStream(), true);) {

            String answer;

            while (true) {
                System.out.println("Choose an operation");
                System.out.println("1 Addion");
                System.out.println("2 Subtraction");
                System.out.println("3 Multiplication");
                System.out.println("4 Division");
                System.out.println("Enter an option");
                int operation = Integer.parseInt(userInput.readLine());

                System.out.println("Enter number");
                int num1 = Integer.parseInt(userInput.readLine());

                System.out.println("Enter number");
                int num2 = Integer.parseInt(userInput.readLine());

                socketOutputWriter.println(operation + ":" + num1 + ":" + num2);

                while ((answer = socketInputReader.readLine()) != null) {
                    System.out.println("Server says: " + answer);
                    System.out.println();
                    break;
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}