初學Java網絡編程socket,寫了個服務器,但是打不開,求指導。代碼如下
import java.io.*;
import java.net.*;
import java.util.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Test2_2Server extends Application{
//創建顯示文本域
private TextArea area = new TextArea();
public void start(Stage primaryStage){
area.setEditable(false);
Scene scene = new Scene(new ScrollPane(area), 450,300);
primaryStage.setTitle("Test2_2Server");
primaryStage.setScene(scene);
primaryStage.show();
try {
//創建一個服務器套接字
ServerSocket serverSocket = new ServerSocket(8800);
Platform.runLater(() ->
area.appendText("Server started at " + new Date() + '\n'));
int clientNo = 1;
while (true){
//監聽
Socket socket = serverSocket.accept();
area.appendText("Starting thread for client " + clientNo + " at " + new Date() + '\n');
//獲取客戶端地址
InetAddress inetAddress = socket.getInetAddress();
area.appendText("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + '\n');
area.appendText("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + '\n');
//創建進程
HandleAClient task = new HandleAClient(socket);
//開始運行
new Thread(task).start();
//客戶端數加一
clientNo++;
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
public static void main(String[] args) {
launch(args);
}
//服務器端的任務類
class HandleAClient implements Runnable{
private Socket socket;
public HandleAClient(Socket socket1){
this.socket = socket1;
}
public void run(){
try {
//創建輸入輸出流
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while (true){
//獲取客戶端數據
double annualInterestRate = inputFromClient.readDouble();
int numberOfYears = inputFromClient.readInt();
double loanAmount = inputFromClient.readDouble();
//計算償還額
double totalPayment = loanAmount * Math.pow(1 + annualInterestRate * 0.01, numberOfYears);
double monthlyPayment = totalPayment / numberOfYears / 12;
//將數據傳送至客戶端
outputToClient.writeDouble(monthlyPayment);
outputToClient.writeDouble(totalPayment);
//輸出
Platform.runLater(() ->{
area.appendText("Annual Interest Rate:" + annualInterestRate + '\n');
area.appendText("Number Of Year:" + numberOfYears + '\n');
area.appendText("Loan Amount:" + loanAmount + '\n');
area.appendText("Monthly Payment:" + monthlyPayment + '\n');
area.appendText("Total Payment:" + totalPayment + '\n');
});
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
已經找到問題,問題出在JavaFX的application線程,需要給服務器也分配一個線程,否則兩者會發生沖突,導致程序無法正常啟動。
具體修改程序代碼如下,由於有人說看起來太復雜,稍作刪減:
import java.io.*;
import java.net.*;
import java.util.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class net extends Application{
//創建顯示文本域
private TextArea area = new TextArea();
public void start(Stage primaryStage){
Scene scene = new Scene(new ScrollPane(area), 450,300);
primaryStage.setTitle("Test2_2Server");
primaryStage.setScene(scene);
primaryStage.show();
//此處做修改,為服務器分配線程
new Thread(() ->{
try {
//創建一個服務器套接字
ServerSocket serverSocket = new ServerSocket(8000);
area.appendText("Server started at " + new Date() + '\n');
while (true){
//監聽
Socket socket = serverSocket.accept();
//創建進程
HandleAClient task = new HandleAClient(socket);
//開始運行
new Thread(task).start();
}
}
catch(IOException ex) {
System.err.println(ex);
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
//服務器端的任務類
class HandleAClient implements Runnable{
private Socket socket;
public HandleAClient(Socket socket1){
this.socket = socket1;
}
public void run(){
try {
//創建輸入輸出流
DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
while (true){
//獲取客戶端數據
double annualInterestRate = input.readDouble();
int numberOfYears = input.readInt();
double loanAmount = input.readDouble();
//計算償還額
double totalPayment = loanAmount * Math.pow(1 + annualInterestRate * 0.01, numberOfYears);
double monthlyPayment = totalPayment / numberOfYears / 12;
//將數據傳送至客戶端
output.writeDouble(monthlyPayment);
output.writeDouble(totalPayment);
//輸出
Platform.runLater(() ->{
area.appendText("Annual Interest Rate:" + annualInterestRate + '\n');
area.appendText("Number Of Year:" + numberOfYears + '\n');
area.appendText("Loan Amount:" + loanAmount + '\n');
area.appendText("Monthly Payment:" + monthlyPayment + '\n');
area.appendText("Total Payment:" + totalPayment + '\n');
});
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}