?
Solved

UDP Instant Messaging Program in Java (Server Side)

Posted on 2004-09-12
2
Medium Priority
?
296 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 750 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 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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
Course of the Month10 days, 3 hours left to enroll

762 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