Passing Objects from Client to Server (and back again)

Hello forum, I having trouble with passing objects from client to server and back again.

a) I have created a client server relationship where by the client creates an instance of an object (player.class), fills it with some data (setPlayersName(String);) then passes it to the server. The Player object needed to implements Serializable before being sent.

b) The server then adds some more data (Vector of game objects) that is then passed back to the client and displayed within a Swing JPanel. Both 憄layer?object as identical on both client and server.

Now a) works fine, I have transferred the object and I have made sure that the data is present with the player object. However, when I try and reverse the process and send the same (serializable) object back to the client I obtain this error.

java.io.NotSerializableException: java.net.Socket

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)

at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)

at SendObjectThread.run(SendObjectThread.java:33)

Without pasting lots of code, would anyone know why it抯 saying that the object is not serializable even though I have successfully passed it in part a) of my application?

Thanks very much for your time,

Harold Clements

[1661 byte] By [haroldclementsa] at [2007-11-27 8:01:38]
# 1

You sent a Player object and all the objects it referred to, and all the objects they referred to, and so on, were all serializable. So you could send it.

Then you changed it somehow.

Now it contains a reference to a Socket, which is not serializable. So you can't send it.

Either change the design so the Player doesn't contain a reference to a Socket, or make that reference transient so that the code doesn't try to serialize a Socket.

DrClapa at 2007-7-12 19:43:45 > top of Java-index,Core,Core APIs...
# 2
Thank you DrClap, The 'transient' declaration was exactly what I needed.Thanks,Harold Clements
haroldclementsa at 2007-7-12 19:43:45 > top of Java-index,Core,Core APIs...