is it possible to send objects like hashtable or vectors through socket.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   chandan_panda
Posted On:   Thursday, May 17, 2001 07:01 AM

is it possible to send objects like hashtable or vectors through socket.
If possible how??

Re: is it possible to send objects like hashtable or vectors through socket.

Posted By:   Tim_Rohaly  
Posted On:   Thursday, May 17, 2001 09:08 AM

Be sure that all the objects stored in the
Vector or Hashtable are serializable.
If not, this will fail. This is what most people have a
problem with...

Re: is it possible to send objects like hashtable or vectors through socket.

Posted By:   John_Tesmer  
Posted On:   Thursday, May 17, 2001 07:56 AM

Absolutely! In J2SE, you can use functionality provided by streams...



You can create a new socket like this:

Socket soc_ClientConnection = new Socket("www.ThisIsMyClient.com", 12345);



This socket can be used to create a stream; it must be defined as an input or output stream like so:

OutputStream os_SocketStream= soc_ClientConnection.getOutputStream();


The getOutputStream() method returns an output stream. The ObjectOutputStream has methods which allow for the writing of serializable objects to this output stream. NOTE that the object must implement Serializable! A vector does implement this interface if I'm not mistaken...


ObjectOuptutStream oos_WriteObjectStream = new ObjectOutputStream(os_SocketStream);


Now, you can use oos_WriteObjectStream's writeObject() method to write objects such as vectors to the output stream. Use readObject() on the other client to reassemble the stream into a meaningful object.


See the API for more information about these methods... I'm writing this in a hurry so I may have left something out!


Hope this helps!

About | Sitemap | Contact