Simple java TCP Chat Server
<p>I am fairly new to java and networking, but I am trying to create a simple command-line based chat server and client. I am working through a tutorial I found and I am trying to make it do what I want it to do. I'll post the server and client, but I really want to focus on the server as that will contain most of the functionality (I think). I have been testing it with telnet so far. </p>

<p>First of all I want to be able to specify an IP Address so I can connect outside of local host. Basically I want to have the user to run the chat by typing something like this: "java ClientChat 10.42.16.12 9020", and the server should send back "Welcome to (user)'s Chat room."</p>

<p>In addition I would like to be able to type in "get" and have it print out everything in the chat buffer. Lastly I would like to be able to type "end" and close the client connection. </p>

<p><strong>Here is the Chat Server:</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);
         // read name
         name  = input.readLine();
         users.add(name); // add to vector
         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 (line.equals("end")) 
                {
                    clients.remove(this);
                    users.remove(name);
                    break;
                }
                if(line.equals("name: " + name))
                {
                    System.out.println("OK");
                }
                boradcast(name,line); // method  of outer class - send messages 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><strong>Here is the Chat Client:</strong></p>

<pre><code>import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.System.out;

public class  TCPClient extends JFrame implements ActionListener 
{
    String uname;
    PrintWriter pw;
    BufferedReader br;
    JTextArea  taMessages;
    JTextField tfInput;
    JButton btnSend,btnExit;
    Socket client;

    public TCPClient(String uname,String servername) throws Exception 
    {
        super(uname);  // set title for frame
        this.uname = uname;
        client = new Socket(servername,9020);
        br = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
        pw = new PrintWriter(client.getOutputStream(),true);
        pw.println(uname);  // send name to server
        buildInterface();
        new MessagesThread().start();  // create thread for listening for messages
    }

    public void buildInterface() 
    {
        btnSend = new JButton("Send");
        btnExit = new JButton("Exit");
        taMessages = new JTextArea();
        taMessages.setRows(10);
        taMessages.setColumns(50);
        taMessages.setEditable(false);
        tfInput  = new JTextField(50);
        JScrollPane sp = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(sp,"Center");
        JPanel bp = new JPanel( new FlowLayout());
        bp.add(tfInput);
        bp.add(btnSend);
        bp.add(btnExit);
        add(bp,"South");
        btnSend.addActionListener(this);
        btnExit.addActionListener(this);
        setSize(500,300);
        setVisible(true);
        pack();
    }

    public void actionPerformed(ActionEvent evt)
    {
        if ( evt.getSource() == btnExit ) 
        {
            pw.println("end");  // send end to server so that server know about the termination
            System.exit(0);
        } else {
            // send message to server
            pw.println(tfInput.getText());
        }
    }

    public static void main(String ... args) 
    {

        // take username from user
        String name = JOptionPane.showInputDialog(null,"Enter your name :", "Username",
             JOptionPane.PLAIN_MESSAGE);
        String servername = "localhost";  
        try 
        {
            new TCPClient(name ,servername);
        } catch(Exception ex) {
            out.println( "Error --&gt; " + ex.getMessage());
        }

    } // end of main

    // inner class for Messages Thread
    class  MessagesThread extends Thread 
    {
        public void run() 
        {
            String line;
            try 
            {
                while(true) 
                {
                    line = br.readLine();
                    taMessages.append(line + "\n");
                } // end of while
            } catch(Exception ex) 
            {   
            }
        }
    }
} 
</code></pre>

<p>I want to get rid of the GUI too but am not sure how to maintain functionality.</p>
