Mikeel Mikeel -4 years ago 97
Java Question

Java - serializable and sending object via socket

I receive an error:

IOException on socket listen: java.io.NotSerializableException: java.net.Socket

i try to send an object via socket with this code :

ObjectOutputStream outObjects = new ObjectOutputStream(socket.getOutputStream());


second line gives error.... but i have serialized ( implements Serializable) the class Player (the class of the currentPlayer object)
but one of the member of Player class is a Socket object.....
i try to redefine and serializeble the socket object but no way to solve the problem....
where am i wrong ?

Answer Source

You can not serialize Socket object, therefore you should make socket field transient. If you need some of socket properties, you can put extra fields to Player to represent these fields.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download