简体   繁体   English

通过套接字发送不可序列化的对象

[英]Sending a non-serializable object through a Socket

I'm trying to send a few Objects around through java.net.Socket and java.net.ServerSocket by using an ObjectOutputStream and ObjectInputStream but I've come across an issue with that. 我试图通过使用ObjectOutputStreamObjectInputStream通过java.net.Socketjava.net.ServerSocket发送一些对象,但是遇到了一个问题。 The objects I'm trying to send aren't serializable. 我尝试发送的对象无法序列化。 I've tried sending them as a String but it can't be cast. 我尝试将它们作为String发送,但是无法强制转换。 Any work-arounds for this issue? 任何解决此问题的方法?

EDIT: 编辑:

I was looking through the StackTrace of the problem, and found that it's being thrown by JLabel. 我正在查看问题的StackTrace,发现它是JLabel抛出的。 I thought that was odd, due to the fact that JLabel implements Serializable. 我认为这很奇怪,因为JLabel实现了Serializable。 But when I checked the JavaDocs through eclipse and came to the line where the error was being thrown by public boolean updateImage(ect..) and realized that it's due to Icon. 但是,当我通过eclipse检查JavaDocs并到达由public boolean updateImage(ect..)引发错误的public boolean updateImage(ect..) ,意识到这是由于Icon引起的。 Is there any way to avoid the issue? 有什么办法可以避免这个问题?

There is a loud warning in the Javadoc of every single Swing component about not serializing Swing components. 在Javadoc中,每个单独的Swing组件都会发出很大的警告,提示您不要序列化Swing组件。 Read it. 阅读。 What you should be doing is serializing the respective models. 您应该做的是序列化各个模型。

You need to implement the Serializable interface mate ;) Take a look here: http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html 您需要实现Serializable接口mate;)在这里看看: http : //docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

You need to implement this interface on the class of the object you are trying to send via socket. 您需要在尝试通过套接字发送的对象的类上实现此接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM