Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chat;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.HashSet;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- //to do: logs
- public class ChatServer {
- //port the server listens on
- private static final int PORT = 9001;
- //set of usernames (so everyone is unique)
- private static HashSet<String> names = new HashSet<String>();
- //set of user inputs
- private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
- JFrame frame;
- JPanel panel;
- JButton button;
- boolean running = true;
- //listens on port and spawns handler methods
- public static void main(String[] args) throws Exception {
- //start server
- new ChatServer();
- //new listener on port
- try (ServerSocket listener = new ServerSocket(PORT)){
- //continuously accept new clients
- while (true) {
- }
- }
- }
- //creates GUI
- ChatServer () {
- frame = new JFrame("Chat Server");
- panel = new JPanel();
- button = new JButton("Close server");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- frame.dispose();
- running = false;
- }
- });
- panel.add(button);
- frame.add(panel);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing (WindowEvent e) {
- frame.dispose();
- running = false;
- }
- });
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- /**
- * A handler thread class. Handlers are spawned from the listening
- * loop and are responsible for a dealing with a single client
- * and broadcasting its messages.
- */
- class Handler extends Thread {
- private String name;
- private Socket socket;
- private BufferedReader in;
- private PrintWriter out;
- //socket equals socket
- public Handler(Socket socket) {
- this.socket = socket;
- }
- //IO
- public void run() {
- try {
- // Create character streams for the socket.
- in = new BufferedReader(new InputStreamReader(
- socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- // Request a name from this client. Keep requesting until
- // a name is submitted that is not already used. Note that
- // checking for the existence of a name and adding the name
- // must be done while locking the set of names.
- while (true) {
- out.println("SUBMITNAME");
- name = in.readLine();
- synchronized (names) {
- if (!names.contains(name)) {
- names.add(name);
- break;
- }
- }
- }
- // Now that a successful name has been chosen, add the
- // socket's print writer to the set of all writers so
- // this client can receive broadcast messages.
- out.println("NAMEACCEPTED");
- writers.add(out);
- // Accept messages from this client and broadcast them.
- // Ignore other clients that cannot be broadcasted to.
- while (true) {
- String input = in.readLine();
- if (input == null) {
- return;
- }
- if (input.equals("DISCONNECTING")) {
- out.println("BYE");
- break;
- }
- for (PrintWriter writer : writers) {
- writer.println("MESSAGE " + name + ": " + input);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } 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);
- }
- try {
- socket.close();
- } catch (IOException e) {
- System.out.println("Socket uncloseable (may be already terminated).");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment