Guest User

ChatServer

a guest
Aug 10th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. package chat;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.PrintWriter;
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13. import java.util.HashSet;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18.  
  19. //to do: logs
  20. public class ChatServer {
  21.  
  22. //port the server listens on
  23. private static final int PORT = 9001;
  24. //set of usernames (so everyone is unique)
  25. private static HashSet<String> names = new HashSet<String>();
  26. //set of user inputs
  27. private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
  28.  
  29. JFrame frame;
  30. JPanel panel;
  31. JButton button;
  32.  
  33. boolean running = true;
  34.  
  35. //listens on port and spawns handler methods
  36. public static void main(String[] args) throws Exception {
  37.  
  38. //start server
  39. new ChatServer();
  40.  
  41. //new listener on port
  42. try (ServerSocket listener = new ServerSocket(PORT)){
  43. //continuously accept new clients
  44. while (true) {
  45.  
  46. }
  47. }
  48. }
  49.  
  50. //creates GUI
  51. ChatServer () {
  52.  
  53. frame = new JFrame("Chat Server");
  54. panel = new JPanel();
  55. button = new JButton("Close server");
  56. button.addActionListener(new ActionListener() {
  57.  
  58. public void actionPerformed(ActionEvent e) {
  59.  
  60. frame.dispose();
  61. running = false;
  62.  
  63. }
  64.  
  65. });
  66. panel.add(button);
  67. frame.add(panel);
  68. frame.addWindowListener(new WindowAdapter() {
  69. public void windowClosing (WindowEvent e) {
  70.  
  71. frame.dispose();
  72. running = false;
  73.  
  74. }
  75. });
  76. frame.pack();
  77. frame.setLocationRelativeTo(null);
  78. frame.setVisible(true);
  79.  
  80.  
  81. }
  82.  
  83. /**
  84. * A handler thread class. Handlers are spawned from the listening
  85. * loop and are responsible for a dealing with a single client
  86. * and broadcasting its messages.
  87. */
  88. class Handler extends Thread {
  89.  
  90. private String name;
  91. private Socket socket;
  92. private BufferedReader in;
  93. private PrintWriter out;
  94.  
  95. //socket equals socket
  96. public Handler(Socket socket) {
  97. this.socket = socket;
  98. }
  99.  
  100. //IO
  101. public void run() {
  102. try {
  103.  
  104. // Create character streams for the socket.
  105. in = new BufferedReader(new InputStreamReader(
  106. socket.getInputStream()));
  107. out = new PrintWriter(socket.getOutputStream(), true);
  108.  
  109. // Request a name from this client. Keep requesting until
  110. // a name is submitted that is not already used. Note that
  111. // checking for the existence of a name and adding the name
  112. // must be done while locking the set of names.
  113. while (true) {
  114. out.println("SUBMITNAME");
  115. name = in.readLine();
  116. synchronized (names) {
  117. if (!names.contains(name)) {
  118. names.add(name);
  119. break;
  120. }
  121. }
  122. }
  123.  
  124. // Now that a successful name has been chosen, add the
  125. // socket's print writer to the set of all writers so
  126. // this client can receive broadcast messages.
  127. out.println("NAMEACCEPTED");
  128. writers.add(out);
  129.  
  130. // Accept messages from this client and broadcast them.
  131. // Ignore other clients that cannot be broadcasted to.
  132. while (true) {
  133. String input = in.readLine();
  134. if (input == null) {
  135. return;
  136. }
  137. if (input.equals("DISCONNECTING")) {
  138. out.println("BYE");
  139. break;
  140. }
  141. for (PrintWriter writer : writers) {
  142. writer.println("MESSAGE " + name + ": " + input);
  143. }
  144. }
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. } finally {
  148. // This client is going down! Remove its name and its print
  149. // writer from the sets, and close its socket.
  150. if (name != null) {
  151. names.remove(name);
  152. }
  153. if (out != null) {
  154. writers.remove(out);
  155. }
  156. try {
  157. socket.close();
  158. } catch (IOException e) {
  159. System.out.println("Socket uncloseable (may be already terminated).");
  160. }
  161. }
  162. }
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment