This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Feb 22, 2003
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?
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.
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.
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 ]
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.
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 ]