Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Writing a Hashtable to a socket

Posted on 2006-03-21
12
Medium Priority
?
802 Views
Last Modified: 2008-03-10
Hello,
I need to know how to write and read hashtables passed over a socket connection. The program basically takes data from a database, converts each row to a hashtable and then needs to connect to several servers, and pipe this information to them. A hashtable for me is the easiest way to do this. Can someone show me how to accomplish this?

Worth 500 points.

Thanks,
Rick
0
Comment
Question by:richardsimnett
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 17

Assisted Solution

by:BogoJoker
BogoJoker earned 600 total points
ID: 16254338
Hi richardsimnett,

A hash table is serializable, you could serialize the table, send the data over the socket, then unserialize the table on the other side.

Joe P
0
 
LVL 92

Expert Comment

by:objects
ID: 16254383
Use ObjectInputStream/ObjectOutputStream
0
 
LVL 92

Accepted Solution

by:
objects earned 1400 total points
ID: 16254400
to send:

ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(map);

too recieve:

ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Map map = (Map) in.readObject();



You'll need to ensure all the objects in your map (keys and values) implement Serializable

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:BogoJoker
ID: 16254408
JAlmanaac on Serializing and Unserializing an object:
http://javaalmanac.com/egs/java.io/SerializeObj.html

Pretty much exactly what objects said.  But the site is a great reference if you did not know about it.

Joe P
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254449
Another approach is to use RMI and it will handle the job of creating sockets/ threads, serializing/ deserializing objects for you. Make sure the object (message) definition is available on both sides (meaning the class whose instances you are putting in the hash-table).
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16254501
I just gotta ask somebody.
Is it me or does the java API look like they are using some CSS and Images now?
I expect both of you have looked at that site tons of times, doesn't it look different! Maybe its just me
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254511
You mean the Java TA here or the Java API doc on Sun's site?
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16254522
Java API, but I was wrong, I was just on the SerializedForm screen, I had never been on it before and there was just a lot of big text and colors...  Normal classes are normal, I was just on a weird page.

Joe P
0
 

Author Comment

by:richardsimnett
ID: 16254548
objects,
Im not familiar with serializable, the keys and values are strings across the board.

Thanks,
Rick
0
 

Author Comment

by:richardsimnett
ID: 16254557
can you give me an example of what this would look like so I can familiarize myself with it?


Thanks,
Rick
0
 

Author Comment

by:richardsimnett
ID: 16254575
nevermind I got it now... Strings are serializable, and that just adds methods that are in place to read / write them.

Im going to split points.

Thank,
Rick
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16254614
>> Im not familiar with serializable, the keys and values are strings across the board.

Primitive data types and many standard Java classes are serializable by default. If that's what you use in your own class, you should be fine
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question