This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error sending serialized object between client and server using sockets

 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an object from class
class MyObject implements Serializable{
...
...
}

and a client with this little snippet in it with the necessary values for ip and port so that a connection is created.

Socket socket = new Socket(ipiString, port);
ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
os.writeObject(new MyObject);
then on my server is this snippet

ServerSocket socket = new ServerSocket(port);
Socket clientSocket = socket.accept();
ObjectOutputStream os = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream is = new ObjectInputStream(clientSocket.getInputStream());
MyObject obj = (MyObject)is.readObject();

Everything goes fine until the object gets read at the server - a ClassCastException is generated... even if I only try to accept the Object object from is.readObject().
I am guessing that the problem is with my object serialization?
Any suggestions?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give us the error message (a ClassCastException always tells you the offending class name, which would be useful for us to know), the stack trace, and the snippet of your code where the stacktrace indicates the problem is?
- Peter
[ May 23, 2003: Message edited by: Peter den Haan ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic