Solved

UDP Instant Messaging Program in Java (Server Side)

Posted on 2004-09-12
2
281 Views
Last Modified: 2010-03-31

I am currently working on an Instant Message Server Client pair using Java

                // Part to determine if username is taken
                if ((regTab.containsKey(key)) == true) {
                    retMes = "Failed - username taken";
                } else {
                   regTab.put(key, data);
                   retMes = "OK";
                }

            // Part of if statement to handle deregistering
            } else if (received.startsWith("offline")) {
                String host = st.nextToken();                
                key = host;
                regTab.remove(key);
               
            // Part of if statement to handle "where" command
            } else if (received.startsWith("where")) {
                String host = st.nextToken();
                key = host;
                String addr = (String)regTab.get(key);
                retMes = "@"+host+":"+addr;

            // Part of if statement to handle "who" command
            } else if (received.startsWith("who")) {
                String whois = "";
                Enumeration enum = regTab.keys();
                while ((enum.hasMoreElements()) == true) {
                    whois = whois + (String)enum.nextElement();
                }
                retMes = whois;

This snipit of code is the part that i'm having difficulty with.. i need several statements that will handle when the client sends a datagram packet with "who", "where". regTab is a hashtable that is storing the user name and the address of the client.

I am having trouble with the who command as it is supposed to take the keys (the user names) from the hashtable and order then in the following and then send them to the client:

username1
username2
...

can anyone help me?
0
Comment
Question by:AnthonyCosenza
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 12040746
to order them, get the keys as an array and use Arrays.sort() method to sort them.

Object[] keys = regTab.keySet().toArray();
Arrays.sort(keys);
for (int i=0; i<keys.length; i++)
{
    whois = whois + keys[i];
}
retMes = whois;
0
 
LVL 2

Assisted Solution

by:gen718
gen718 earned 250 total points
ID: 12041145
Use a TreeMap instead of a HashMap.
The TreeMap will keep the collection in sorted order by key.

You can create a class which implements the Comparator interface in order to have control over the sorting of the keys. Pass this Comparator object in the TreeMap instantation.

Check HorstMann's Core Java 2 book for details on TreeMap usage.

Good Luck :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 82
netstat -ano | find "8000" and taskkill /f /pid 2984 3 55
servlet  URL Rewriting 1 36
runtime exception 2 27
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now