Solved

UDP Instant Messaging Program in Java (Server Side)

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

708 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

13 Experts available now in Live!

Get 1:1 Help Now