Solved

UDP Instant Messaging Program in Java (Server Side)

Posted on 2004-09-12
2
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jmss example java 2 48
add projects t working set in maven 2 66
Selenium docs api java index 3 97
running on tomcat not jboss eap 7.0 3 52
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 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