简体   繁体   中英

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. The objects I'm trying to send aren't serializable. I've tried sending them as a String but it can't be cast. 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. I thought that was odd, due to the fact that JLabel implements 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. 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. 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

You need to implement this interface on the class of the object you are trying to send via socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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