我制作了一个简单的客户端-服务器应用程序,其中服务器将对象的ArrayList发送给客户端。问题是,当我尝试执行此操作时,服务器上(在我写对象的行中)出现 Broken Pipe 错误,并且出现 StreamCorruptedException:意外的块数据在我所阅读的行上的客户端错误。
这是服务器部分:
try {
ObjectOutputStream objectOutput = new ObjectOutputStream(incoming.getOutputStream());
objectOutput.writeObject(arr);
} catch (IOException e) {
System.out.println("Failed to send ArrayList");
e.printStackTrace();
}
这是客户:
ObjectInputStream inStream = new ObjectInputStream(s.getInputStream());
email = (ArrayList<Email>) inStream.readObject();
//Casting the arrayList
emailList = FXCollections.observableArrayList(email);
//Sorting the emails
Collections.sort(emailList, (Email o1, Email o2) -> {
if (o1.getData() == null || o2.getData() == null) {
return 0;
}
return o1.getData().compareTo(o2.getData());
});
这是我在客户端上遇到的完整错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.StreamCorruptedException: unexpected block data
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1581)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2278)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2158)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2060)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at java.util.ArrayList.readObject(ArrayList.java:797)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1158)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2169)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2060)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427)
at mailbox.DataModel.loadData(DataModel.java:69)
at mailbox.ListController.initModel(ListController.java:28)
at mailbox.MailBox.start(MailBox.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application mailbox.MailBox
这是我要发送的对象的类:
public class Email implements Serializable {
private final IntegerProperty id = new SimpleIntegerProperty();
public final IntegerProperty IDProperty() {
return this.id;
}
public final Integer getID() {
return this.IDProperty().get();
}
public final void setID(final Integer id) {
this.IDProperty().set(id);
}
private final StringProperty mittente = new SimpleStringProperty();
public final StringProperty MittenteProperty() {
return this.mittente;
}
public final String getMittente() {
return this.MittenteProperty().get();
}
public final void setMittente(final String mittente) {
this.MittenteProperty().set(mittente);
}
private final StringProperty destinatario = new SimpleStringProperty();
public final StringProperty DestinatarioProperty() {
return this.destinatario;
}
public final String getDestinatario() {
return this.DestinatarioProperty().get();
}
public final void setDestinatario(final String destinatario) {
this.DestinatarioProperty().set(destinatario);
}
private final StringProperty oggetto = new SimpleStringProperty();
public final StringProperty OggettoProperty() {
return this.oggetto;
}
public final String getOggetto() {
return this.OggettoProperty().get();
}
public final void setOggetto(final String oggetto) {
this.OggettoProperty().set(oggetto);
}
private final StringProperty testo = new SimpleStringProperty();
public final StringProperty TestoProperty() {
return this.testo;
}
public final String getTesto() {
return this.TestoProperty().get();
}
public final void setTesto(final String testo) {
this.TestoProperty().set(testo);
}
private final ObjectProperty<Date> data = new SimpleObjectProperty<Date>();
public final ObjectProperty<Date> DataProperty() {
return this.data;
}
public final Date getData() {
return this.data.get();
}
public final void setData(final Date data) {
this.data.set(data);
}
public Email(int id, String mittente, String destinatario, String oggetto, String testo, Date data) {
setID(id);
setMittente(mittente);
setDestinatario(destinatario);
setOggetto(oggetto);
setTesto(testo);
setData(data);
}
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeInt(getID());
out.writeUTF(getMittente());
out.writeUTF(getDestinatario());
out.writeUTF(getOggetto());
out.writeUTF(getTesto());
out.writeObject(getData());
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
try {
Field field = this.getClass().getDeclaredField("id");
field.setAccessible(true);
field.set(this, new SimpleIntegerProperty());
field = this.getClass().getDeclaredField("mittente");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());
field = this.getClass().getDeclaredField("destinatario");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());
field = this.getClass().getDeclaredField("oggetto");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());
field = this.getClass().getDeclaredField("testo");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());
field = this.getClass().getDeclaredField("data");
field.setAccessible(true);
field.set(this, new SimpleObjectProperty<Date>());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IOException(e);
}
setID(in.readInt());
setMittente(in.readUTF());
setDestinatario(in.readUTF());
setOggetto(in.readUTF());
setTesto(in.readUTF());
setData((Date) in.readObject());
}
}
我读到此错误是由于读取对象时流已关闭而引起的。我该如何预防?有没有办法强制套接字始终可用?