我正在学习一些基本的Java网络知识,并且试图使我学到的东西变为现实,因此,当我在同一台计算机上运行服务器和客户端类时,它可以正常运行,但是当我将客户端项目转移到另一个计算机上时计算机并在运行服务器后运行项目,它将冻结并打印连接超时语句。
这是我的服务器代码
package sample;
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;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
TextArea ta = new TextArea();
primaryStage.setTitle("server");
primaryStage.setScene(new Scene(new ScrollPane(ta), 450, 200));
primaryStage.show();
new Thread(()->{
try {
ServerSocket ss = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server started at " + new Date() + '\n'));
Socket s = ss.accept();
DataInputStream inputFromClient = new DataInputStream(s.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(s.getOutputStream());
while (true) {
double radius = inputFromClient.readDouble();
double area = radius * radius * Math.PI;
outputToClient.writeDouble(area);
Platform.runLater(() -> {
ta.appendText("Radius received from client: "
+ radius + '\n');
ta.appendText("Area is: " + area + '\n');
});
}
} catch (Exception e){
e.printStackTrace();
}
}).start();
}
public static void main(String[] args) {
launch(args);
}
}
扬帆大鱼
杨__羊羊
相关分类