Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Server:
- public class Server {
- private static final int PORT = 9001;
- private static HashSet<String> names = new HashSet<String>();
- private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
- public static void main(String[] args) throws Exception {
- ServerSocket listener = new ServerSocket(PORT);
- System.out.println("The chat server is running. PORT = " + listener.getLocalPort());
- try {
- while (true) {
- new Handler(listener.accept()).start();
- }
- } finally {
- listener.close();
- }
- }
- private static class Handler extends Thread {
- private String name;
- private Socket socket;
- private BufferedReader in;
- private PrintWriter out;
- public Handler(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- try {
- in = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- while (true) {
- out.println("SUBMITNAME");
- name = in.readLine();
- if (name == null) {
- return;
- }
- synchronized (names) {
- if (!names.contains(name)) {
- names.add(name);
- System.out.println(name + " has joined the chat!" + socket.getInetAddress().getHostAddress());
- out.println("NAMEACCEPTED");
- writers.add(out);
- for (PrintWriter writer : writers) {
- writer.println("MESSAGE " + name + " has joined the chat!");
- }
- break;
- } else {
- writers.add(out);
- out.println("NAMEACCEPTED");
- }
- }
- }
- while (true) {
- String input = in.readLine();
- if (input == null) {
- return;
- }
- for (PrintWriter writer : writers) {
- writer.println("MESSAGE " + name + ": " + input);
- }
- }
- } catch (IOException e) {
- System.out.println(e);
- } finally {
- // This client is going down! Remove its name and its print
- // writer from the sets, and close its socket.
- if (name != null) {
- names.remove(name);
- }
- if (out != null) {
- writers.remove(out);
- }
- System.out.println(name + " has left the chat!");
- for (PrintWriter writer : writers) {
- writer.println("MESSAGE " + name + " has left the chat!");
- }
- try {
- socket.close();
- } catch (IOException e) {
- }
- }
- }
- }
- }
- Client:
- public class Client {
- BufferedReader in;
- PrintWriter out;
- JFrame frame = new JFrame("Chatter");
- JTextField textField = new JTextField(40);
- JTextArea messageArea = new JTextArea(8, 40);
- public Client() {
- // Layout GUI
- textField.setEditable(false);
- 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) {
- out.println(textField.getText());
- textField.setText("");
- }
- });
- }
- /**
- * Prompt for and return the address of the server.
- */
- private String getServerAddress() {
- return JOptionPane.showInputDialog(
- frame,
- "Enter IP Address of the Server:",
- "Welcome to the Chatter",
- JOptionPane.QUESTION_MESSAGE);
- }
- /**
- * Prompt for and return the desired screen name.
- */
- private String getName() {
- return JOptionPane.showInputDialog(
- frame,
- "Choose a screen name:",
- "Screen name selection",
- JOptionPane.PLAIN_MESSAGE);
- }
- /**
- * Connects to the server then enters the processing loop.
- */
- private void run() throws IOException {
- // Make connection and initialize streams
- String serverAddress = getServerAddress();
- Socket socket = new Socket(serverAddress, 9001);
- in = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- // Process all messages from server, according to the protocol.
- while (true) {
- String line = in.readLine();
- if (line.startsWith("SUBMITNAME")) {
- out.println(getName());
- } else if (line.startsWith("NAMEACCEPTED")) {
- textField.setEditable(true);
- } else if (line.startsWith("MESSAGE")) {
- messageArea.append(line.substring(8) + "\n");
- }
- }
- }
- /**
- * Runs the client as an application with a closeable frame.
- */
- public static void main(String[] args) throws Exception {
- Client client = new Client();
- client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- client.frame.setVisible(true);
- client.run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement