wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes ObjectInputStream and Vector question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ObjectInputStream and Vector question?" Watch "ObjectInputStream and Vector question?" New topic
Author

ObjectInputStream and Vector question?

Anthony Yulo
Ranch Hand

Joined: Feb 22, 2003
Posts: 52
Since the Vector class implements the Serializable interface, it can be put into the ObjectInputStream and ObjectOutputStream right?
but how about when the elements of the Vector is does not implement Serializable?


it's alright, the gun isn't loaded... - hey
Anthony Yulo
Ranch Hand

Joined: Feb 22, 2003
Posts: 52
For example, i'm implementing a chat program using sockets... From the server, I want to pass the list of all online users to all of the clients.. so I'm planning to pass a Vector of Sockets so the clients can keep track of the internet addresses of other clients and some other info... so I would put the Vector of Sockets in an ObjectOutputStream...
Well actually myprogram generated an IOException... i just called ObjectOutputStream.writeObject() function and ObjectinPutStream.readObject() on the client.. but did not work... or i just need additional code to process it...
Since Vector is Serializable but Socket is not Serializable, is this supposed to be oK? or do i have to make another class that implements Serializable and then encapsulate the ClientInformation?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
First, Vector is evil. Use ArrayList unless you have misfortune to be trapped with JDK 1.1. This doesn't really have much to do with your question, but it's still worth mentioning, IMO.
If a Vector or other Collection contains elements which are not Serializable, you'll get a NotSerializableException.
or do i have to make another class that implements Serializable and then encapsulate the ClientInformation?
Yeah, you'd want to store the pertinent info in a Serializable class, and later create a new Socket using that info.


"I'm not back." - Bill Harding, Twister
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
And you've probably deduced this, but if your encapsulating class is made up of other objects, those need to be serializable, also. It goes down the chain.
Though I have experienced a time or two where something buried that wasn't serializable didn't cause an exception to be thrown, but my data was all wacky when the "main" object was deserialized.
Anthony Yulo
Ranch Hand

Joined: Feb 22, 2003
Posts: 52
ok.. tnx to you 2 for the englightenment...
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
or do i have to make another class that implements Serializable and then encapsulate the ClientInformation?[/QB]

Can you elaborate it a bit please. i,am trying to work out a similar thing. The socket.accept() method returns a Socket object .
Now as the clients get connected to the server, the the Socket.accept() function would create a socket for each connected client.
Now if these Socket objects are stored in a file and then retrived so as to enable monitoring of the connected clients . Also these Socket objects can then used for direct commnunication( that is the list of Socket objects would enable the the server admin to select any connected client to have a one to one communication).
Do reply
Thanks in advance
[ April 25, 2003: Message edited by: raghav mathur ]

Raghav.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
java.net.Socket is not Serializable and for a reason. The Socket represents a live connection and thus cannot be serialized. The only workaround is to read the necessary info from the Socket object, serialize that info and make a new connection later on using that information.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
Originally posted by Lasse Koskela:
java.net.Socket is not Serializable and for a reason. The Socket represents a live connection and thus cannot be serialized. The only workaround is to read the necessary info from the Socket object, serialize that info and make a new connection later on using that information.

My idea of of asking this question is that i need to implement a chat server . As the clients get connected ,the Scoket object is created on the server side . Now the next step would be to open the I/O streams on the Socket Object
I want to hold on a bit ..... i mean , i don't want to open I/O streams on a socket till the time i select a client whom i wish to talk to .
Suppose there are 5 clients connecetd and i wish to talk to the 4th one . So for this purpose i need to store the Socket Objects of all the 5 clients somewhere so that a list of connected clients can be mantained and on the basis of selection , an Input and Output streams for a specific Socket object ( the 4th one in this case)can be opened.
What can be done to achieve this kind of an objective.
one more thing ..... when this application would run on a web server ( say on www.xyz.com)then how
would the Socket objects or anything be written to a file which would reside on the Web server.
(not on the Local System)
Thanks in advance
[ April 25, 2003: Message edited by: raghav mathur ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ObjectInputStream and Vector question?