Solved

UDP Instant Messaging Program in Java (Server Side)

Posted on 2004-09-12
2
286 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 60
Running JavaFX on JDeveloper 12C 1 55
Java program running SQL query 5 38
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

820 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