Simple Java Chat Server
<p>I am making a TCP chat server from a simple tutorial I found and I'm kind of new to java. I will eventually make a client class but I've been testing it through telnet so far. I have a couple of problems. I have setup the server to take commands from the client. For example "EXIT" which closes the client connection, and "Username" which prints "OK". Shown below: </p>

<p><strong>USER 1: TIM</strong></p>

<pre><code>Welcome to Bob's Chat Server!

Please Enter a User Name: 
Tim
Welcome Tim we hope you enjoy your chat today
Bob:Hello
Hi Bob
Tim
OK
EXIT
Closing Connection  . . . Goodbye
</code></pre>

<p><strong>USER 2: BOB</strong></p>

<pre><code>Welcome to Bob's Chat Server!

Please Enter a User Name: 
Bob
Welcome Bob we hope you enjoy your chat today
Hello
Tim:Hi Bob
Tim:Tim
</code></pre>

<p>I have three problems that I want to address: </p>

<ol>
<li><p>You'll notice that when USER 1 typed "Tim" (his username) it said "OK" like I wanted, but it also printed "Tim" to USER 2. Is there a way to make it not send my typed commands across? So when I type "Tim" it doesn't print "Tim" to USER 2? </p></li>
<li><p>When messages are sent to other users it displays who said it. Is there a way to print the name on both connections? For example when USER 2 says "Hello" it looks more like "Bob: Hello" on both connections? </p></li>
<li><p>Is there a way to keep track of everything that's said in the entire chat session and print off the entire contents of the chat to whatever user requested it? </p></li>
</ol>

<p><strong>Here is my server code:</strong></p>

<pre><code>// Chat Server runs at port no. 9020
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;

public class  TCPServer 
{
  Vector&lt;String&gt; users = new Vector&lt;String&gt;();
  Vector&lt;HandleClient&gt; clients = new Vector&lt;HandleClient&gt;();

  int PORT = 9020;
  int NumClients = 10;

  public void process() throws Exception  
  {
      ServerSocket server = new ServerSocket(PORT,NumClients);
      out.println("Server Connected...");
      while( true) 
      {
         Socket client = server.accept();
         HandleClient c = new HandleClient(client);
         clients.add(c);
     }  // end of while
  }

  public static void main(String ... args) throws Exception 
  {
      new TCPServer().process();
  } // end of main

  public void boradcast(String user, String message)  
  {
        // send message to all connected users
        for (HandleClient c : clients)
           if (!c.getUserName().equals(user))
           {
              c.sendMessage(user,message);
           }
  }

  class HandleClient extends Thread 
  {
    String name = "";
    BufferedReader input;
    PrintWriter output;

    public HandleClient(Socket client) throws Exception 
    {
          // get input and output streams
         input = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
         output = new PrintWriter (client.getOutputStream(),true);
         output.println("Welcome to Bob's Chat Server!\n");
         // read name
         output.println("Please Enter a User Name: ");
         name  = input.readLine();
         users.add(name); // add to vector
         output.println("Welcome "+name+" we hope you enjoy your chat today");
         start();
    }

    public void sendMessage(String uname,String  msg)  
    {
        output.println( uname + ":" + msg);
    }

    public String getUserName() 
    {  
        return name; 
    }

    public void run()  
    {
         String line;
         try    
         {
            while(true)   
            {
                        line = input.readLine();
                if("EXIT".equals(line))
            {
                output.println("Closing Connection  . . . Goodbye");
                            clients.remove(this);
                        users.remove(name);
                break;
                    }
            else if(name.equals(line))
            {
                            output.println("OK");
                    }
                boradcast(name,line); // method  of outer class - send messages to all
            }// end of while
         } // try
         catch(Exception e) 
         {
           System.out.println(e.getMessage());
         }
    } // end of run()
  } // end of inner class
} // end of Server
</code></pre>

<p>Any help is appreciated. Thanks. </p>
