为什么这个 Java 套接字客户端不起作用?
Why didn't this Java socket client work?
客户端代码
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:
但是我不知道为什么,为什么?
ObjectInputStream 读取流的 header 以检查它是否是 Object 流。在客户端中它正在等待服务器发送 header 并且在服务器中它正在等待客户端发送 header.
您需要先创建输出并刷新它,以便另一端读取 header。
客户端代码
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:
但是我不知道为什么,为什么?
ObjectInputStream 读取流的 header 以检查它是否是 Object 流。在客户端中它正在等待服务器发送 header 并且在服务器中它正在等待客户端发送 header.
您需要先创建输出并刷新它,以便另一端读取 header。