document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package Chatroom;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintWriter;
  10. import java.net.Socket;
  11.  
  12. import javax.swing.*;
  13.  
  14. /*
  15.  * This special client is made for debugging purposes. It allows to enter a custom message and send it as is to the server. By following the
  16.  * communication protocol, it is possible to simulate every client action and verify that the server correctly processes the response.
  17.  * @version 0.1
  18.  */
  19. public class AdminClient {
  20.    
  21.     BufferedReader in;
  22.     PrintWriter out;
  23.     JFrame frame = new JFrame("Chatter");
  24.     JTextField textField = new JTextField(40);
  25.     JTextArea messageArea = new JTextArea(8, 40);
  26.    
  27.     /**
  28.      * Constructs the client by laying out the GUI and registering a listener with the
  29.      * textfield so that pressing Enter in the listener sends the textfield contents
  30.      * to the server. This textfield is initially not editable, and only becomes
  31.      * editable after the client receives the NAMEACCEPTED message from the server.
  32.      */
  33.     public AdminClient() {
  34.        
  35.         //Layout GUI
  36.         textField.setEditable(true);
  37.         messageArea.setEditable(false);
  38.         frame.getContentPane().add(textField, "North");
  39.         frame.getContentPane().add(new JScrollPane(messageArea), "Center");
  40.         frame.pack();
  41.        
  42.         //Add listeners
  43.         textField.addActionListener(new ActionListener() {
  44.             public void actionPerformed(ActionEvent e) { //When pressed ENTER in the textfield, this is triggered
  45.                 out.println(textField.getText());
  46.                 textField.setText("");
  47.             }
  48.         });
  49.     }
  50.    
  51.     /**
  52.      * Ask for the server IP address that the user wants to connect to
  53.      */
  54.     private String getServerAddress() {
  55.         return JOptionPane.showInputDialog(frame, "Enter IP of server:", "Welcome to the chatbox",
  56.                 JOptionPane.QUESTION_MESSAGE);
  57.     }
  58.    
  59.     /**
  60.      * Ask for the desired username.
  61.      */
  62.     private String getName() {
  63.         return JOptionPane.showInputDialog(frame, "Choose a screen name:", "Username required",
  64.                 JOptionPane.PLAIN_MESSAGE);
  65.     }
  66.    
  67.     /**
  68.      * Connects to the server then enters the processing loop.
  69.      */
  70.     public void run() throws IOException {
  71.         String serverAddress = getServerAddress();
  72.         Socket socket = new Socket(serverAddress, 9001);
  73.         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  74.         out = new PrintWriter(socket.getOutputStream(), true);
  75.        
  76.         //Process all server messages, according to the protocol
  77.         while (true) {
  78.             String line = in.readLine();
  79.                 messageArea.append(line + "\\n"); //Start message at character 8(after the protocol word MESSAGE)
  80.         }
  81.     }
  82.      
  83.      public static void main(String[] args) throws Exception {
  84.             AdminClient client = new AdminClient();
  85.             client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.             client.frame.setVisible(true);
  87.             client.run();
  88.          
  89.          }
  90.      
  91.      
  92. }
');