Win a copy of Clojure in Action this week in the Clojure 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
  • 0
  • 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
  • 0
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic