为什么这个Java套接字客户端没有工作?

时间:2016-08-20 10:56:41

标签: java sockets java-io

客户端代码

try {

    Socket socket = new Socket("localhost", 8888);
    response = new ObjectInputStream(socket.getInputStream());
    request = new ObjectOutputStream(socket.getOutputStream());

    //  while (true) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter annual interest rate: ");
    Double rate = input.nextDouble();

    System.out.print("Enter number of years: ");
    int numOfYears = input.nextInt();

    System.out.print("Enter loan amount: ");
    int loanAmount = input.nextInt();

    request.writeObject(new Loan(rate, numOfYears, loanAmount));
    request.flush();

    Loan loan = (Loan)response.readObject();
    double monthlyPayment = loan.getMonthlyPayment();
    double totalPayment = loan.getTotalPayment();
    System.out.println("Total payment: " + totalPayment);
    System.out.println("Monthly payment: " + monthlyPayment);
    //  }
}
catch (ClassNotFoundException ex) {
    ex.printStackTrace();
}
catch (IOException ex) {
    System.out.println(ex);
}
finally {
    try {
        response.close();
        request.close();
    }
    catch (IOException ex) {
        ex.printStackTrace();
    }
}

服务器代码

try {
    ServerSocket ss = new ServerSocket(8888);
    System.out.println("Server started at " + new Date());

    while (true) {
        Socket socket = ss.accept();
        InetAddress ia = socket.getInetAddress();
        System.out.println("Client " + ia.getHostName() + "/" + ia.getHostAddress() + " connected at " + new Date());

        HandleAClient task = new HandleAClient(socket);
        new Thread(task).start();
    }
} catch (IOException ex) {
    System.err.println(ex);
}

class HandleAClient implements Runnable {
    private Socket socket;
    private ObjectInputStream input;
    private ObjectOutputStream output;

    public HandleAClient(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            input = new ObjectInputStream(socket.getInputStream());
            output = new ObjectOutputStream(socket.getOutputStream());

            while(true) {
                Object object = input.readObject();
                Loan loan = (Loan)object;

                double annualInterestRate = loan.getRate();
                int numOfYears = loan.getNumOfYears();
                int loanAmount = loan.getLoanAmount();

                //  computePayment(annualInterestRate, numOfYears, loanAmount);
                double totalPayment = loanAmount*annualInterestRate/100*numOfYears + loanAmount;
                double monthlyPayment = totalPayment/numOfYears/12;

                loan.setTotalPayment(totalPayment);
                loan.setMonthlyPayment(monthlyPayment);
                output.writeObject(loan);
                output.flush();
            }
        }
        catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        catch (IOException ex) {
            System.err.println(ex);
        }
        finally {
            try {
                input.close();
                output.close();
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

输出

服务器:

Server started at Sat Aug 20 18:53:20 CST 2016
Client localhost/127.0.0.1 connected at Sat Aug 20 18:53:29 CST 2016

但是客户没有任何输出。怎么了?

最后我找到了解决方案,我刚刚更改了以下代码:

                Socket socket = new Socket("localhost", 8888);
                request = new ObjectOutputStream(socket.getOutputStream());
                response = new ObjectInputStream(socket.getInputStream());

然后客户端工作正常:

Enter annual interest rate: 4.9
Enter number of years: 10
Enter loan amount: 99
Total payment: 147.51
Monthly payment: 1.22925
Enter annual interest rate:

但我不知道为什么,为什么?

1 个答案:

答案 0 :(得分:4)

ObjectInputStream读取流的标头以检查它是否为对象流。在客户端,它正在等待服务器发送标头,并在服务器中等待客户端发送标头。

您需要先创建输出并将其刷新,以便有另一端的标题可供阅读。