• Post Reply Bookmark Topic Watch Topic
  • New Topic

NotSerializableException with java.net.Socket

 
adiyta tvameka
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I want to serialize the socket connection itself, but while running the program iam getting Not Serializable Exception..

Part of my program is :

public Socket serializeSocket(Socket socket)
{
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream stream = null;
ObjectInputStream istream = null;
try
{
fos = new FileOutputStream("soc.out");
stream = new ObjectOutputStream(fos);
stream.writeObject(socket);

fis = new FileInputStream("soc.out");
istream = new ObjectInputStream(fis);
socket = (Socket)istream.readObject();
}
catch(IOException ie)
{
ie.printStackTrace();
}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
return socket;
}

Thanks In Advance
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IIRC, objects to Sockets and Files can not ever be serialized. If you could serialize a Socket, that would mean when you run the program again, the socket would be able to automatically be reconnected to the server, which doesn't make a lot of sense, to me at least. Ditto for a file handle. What if the file no longer exists?

You can serialize an object that stores things like IP addresses, ports, paths to files ect.

Could be wrong though
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know that you just can serialize objects that implement the java.io.Serializable interface, do you not?

Does Socket or ServerSocket implement serializable?
 
Jamie Cash
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Socket does not implement serializable.

You can test if a class implements Serializable by running serialver -show and entering the class name.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!