Witam. Mam taki kod serwera:
@Override
public void run() {
try {
serverSocket = new ServerSocket(9998);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("connected");
input = new ObjectInputStream(socket.getInputStream());
Message ping = (Message) input.readObject();
System.out.println(ping.getMessage());
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
I klienta:
@Override
public void start(Stage primaryStage) throws Exception {
socket = new Socket("localhost", 9998);
System.out.println("connected");
output = new ObjectOutputStream(socket.getOutputStream());
output.writeObject(new Message(CONFIG, "ping"));
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/window/fxmls/window.fxml"));
loader.setController(this);
AnchorPane anchorPane = loader.load();
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.show();
}
Gdy odpalam serwer, a następnie klienta, dostaję błąd w kliencie:
"D:\Program Files\Programs\Java\jdk-10.0.2\bin\java.exe" "-javaagent:D:\Program Files\Programs\JetBrains\IntelliJ IDEA 2018.3.3\lib\idea_rt.jar=64885:D:\Program Files\Programs\JetBrains\IntelliJ IDEA 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Kuba\Desktop\Odbiorca1\out\production\Odbiorca1 logic.Window
connected
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.io.NotSerializableException: logic.message.Message
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at logic.Window.start(Window.java:82)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Process finished with exit code 1
I w serwerze:
"D:\Program Files\Programs\Java\jdk-10.0.2\bin\java.exe" "-javaagent:D:\Program Files\Programs\JetBrains\IntelliJ IDEA 2018.3.3\lib\idea_rt.jar=64866:D:\Program Files\Programs\JetBrains\IntelliJ IDEA 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Kuba\Desktop\AplikacjaServer2\out\production\AplikacjaServer2 classes.app.Main
connected
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: logic.message.Message
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1591)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at classes.app.Connector.run(Connector.java:49)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.io.NotSerializableException: logic.message.Message
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at logic.Window.start(Window.java:82)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Jak można to naprawić? Wcześniej mi działało przesyłanie klasy Message.
A i klasa Message (ta które jest przesyłana):
package logic.message;
public class Message {
private Header header;
private String message;
private String recipient;
private String author;
public Message(Header header, String message) {
this.header = header;
this.message = message;
}
public Message(Header header, String message, String author) {
this.header = header;
this.message = message;
this.author = author;
}
public Message(Header header, String message, String recipient, String author) {
this.header = header;
this.message = message;
this.recipient = recipient;
this.author = author;
}
public Header getHeader() {
return header;
}
public String getMessage() {
return message;
}
public String getRecipient() {
return recipient;
}
public String getAuthor() {
return author;
}
}
Z góry dzięki za pomoc ;)