Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Chatroom;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import javax.swing.*;
- /*
- * 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
- * communication protocol, it is possible to simulate every client action and verify that the server correctly processes the response.
- * @version 0.1
- */
- public class AdminClient {
- BufferedReader in;
- PrintWriter out;
- JFrame frame = new JFrame("Chatter");
- JTextField textField = new JTextField(40);
- JTextArea messageArea = new JTextArea(8, 40);
- /**
- * Constructs the client by laying out the GUI and registering a listener with the
- * textfield so that pressing Enter in the listener sends the textfield contents
- * to the server. This textfield is initially not editable, and only becomes
- * editable after the client receives the NAMEACCEPTED message from the server.
- */
- public AdminClient() {
- //Layout GUI
- textField.setEditable(true);
- messageArea.setEditable(false);
- frame.getContentPane().add(textField, "North");
- frame.getContentPane().add(new JScrollPane(messageArea), "Center");
- frame.pack();
- //Add listeners
- textField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) { //When pressed ENTER in the textfield, this is triggered
- out.println(textField.getText());
- textField.setText("");
- }
- });
- }
- /**
- * Ask for the server IP address that the user wants to connect to
- */
- private String getServerAddress() {
- return JOptionPane.showInputDialog(frame, "Enter IP of server:", "Welcome to the chatbox",
- JOptionPane.QUESTION_MESSAGE);
- }
- /**
- * Ask for the desired username.
- */
- private String getName() {
- return JOptionPane.showInputDialog(frame, "Choose a screen name:", "Username required",
- JOptionPane.PLAIN_MESSAGE);
- }
- /**
- * Connects to the server then enters the processing loop.
- */
- public void run() throws IOException {
- String serverAddress = getServerAddress();
- Socket socket = new Socket(serverAddress, 9001);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- //Process all server messages, according to the protocol
- while (true) {
- String line = in.readLine();
- messageArea.append(line + "\n"); //Start message at character 8(after the protocol word MESSAGE)
- }
- }
- public static void main(String[] args) throws Exception {
- AdminClient client = new AdminClient();
- client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- client.frame.setVisible(true);
- client.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement