我正在使用JavaFX设置客户端/服务器应用程序。为了创建UI,我使用了SceneBuilder。
我对服务器类以及作为简单行(readUTF,writeUTF)发送和接收数据没有任何问题。我决定更进一步-在客户端和服务器之间发送和接收对象。这里我有一个问题。当我删除初始化ObjectInputStream的行时,程序运行良好,但如果GUI不能打开,并且关闭服务器,则会出现以下异常。
简而言之: 由于GUI无法打开,因此无法使用initialize方法初始化ObjectInputStream。
MainClient.class:
public class MainClient extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("page/client.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Hello World");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller类中的初始化方法:
public void initialize(URL location, ResourceBundle resources) {
try {
socket = new Socket("localhost", 7575);
//out = new DataOutputStream(socket.getOutputStream());
//in = new DataInputStream(socket.getInputStream());
bos = new BufferedOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(bos);
bis = new BufferedInputStream(socket.getInputStream());
ois = new ObjectInputStream(bis);
}
catch(IOException e) {
e.printStackTrace();
}
}
例外说明:
Picked up JAVA_TOOL_OPTIONS: -Djava.io.tmpdir=/home/oska/.var/app/com.jetbrains.IntelliJ-IDEA-Community/cache/tmp/
Gtk-Message: 13:38:07.490: Failed to load module "atk-bridge"
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2681)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at controller.ControllerClient.initialize(ControllerClient.java:43)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at MainClient.start(MainClient.java:11)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$424(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$404(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$402(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$403(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$208(GtkApplication.java:245)
at java.lang.Thread.run(Thread.java:748)