Skip to content
Advertisement

Send Object Over Java Socket

I have a class name player

JavaScript

(It is for creating a new player)

I also have a client class:

JavaScript

And im using objectOutputStream but it says that it

“java.io.NotSerializableException: objects.Player”

And I want to send my player to the server but it says that exception!

Also if you need here is the server class

JavaScript

and also if you want here are the lines of code that send the request to the client class

JavaScript

Advertisement

Answer

Looks like you forgot to make the Player object Serializable, thus code is throwing java.io.NotSerializableException

If you need to send some object over network then the object need to be Serializable.

Serialization is the process of taking the memory data structure of an object and encoding it into a serial (hence the term) sequence of bytes. This encoded version can then be saved to disk, sent across a network connection, or otherwise communicated to a recipient. (from Wikipedia.org)

I have updated the code

Player.java

JavaScript

Cliente.java

JavaScript

Servidor.java

JavaScript

Compiling the code:

JavaScript

Run:

JavaScript

Output appearing on Servidor console:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement