• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ObjectInputStream and Vector question?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Anthony Yulo
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. tnx to you 2 for the englightenment...
 
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic