wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes error sending serialized object between client and server using sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "error sending serialized object between client and server using sockets" Watch "error sending serialized object between client and server using sockets" New topic
Author

error sending serialized object between client and server using sockets

Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108
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?


SCJP 1.2, SCJP 5, SCBCD
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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.
 
subject: error sending serialized object between client and server using sockets
 
Similar Threads
Server / Client with Threads
I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time
Serialization in multi-player game
How can Socket.getInputStream() block
I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time