Advertisement
Guest User

Untitled

a guest
Jan 14th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.net.*;
  3. import java.util.ArrayList;
  4. import java.util.regex.Pattern;
  5. import java.io.*;
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextArea;
  8. import javax.swing.JTextField;
  9.  
  10. public class ChatServer implements Runnable {
  11. private ChatServerThread clients[] = new ChatServerThread[50];
  12. private ServerSocket serverSocket = null;
  13. private Thread t = null;
  14. private int noOfClients = 0;
  15. private JTextArea chatField;
  16. private JTextField typeField;
  17.  
  18. public ChatServer(int port) {
  19. JFrame frame;
  20. typeField = new JTextField(160);
  21. chatField = new JTextArea();
  22.  
  23. frame = new JFrame();
  24. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. frame.setLayout(new BorderLayout());
  26. frame.setTitle("Server");
  27. frame.setSize(500, 300);
  28.  
  29. frame.add(chatField, BorderLayout.CENTER);
  30.  
  31. chatField.setEditable(false);
  32. frame.setVisible(true);
  33.  
  34. try {
  35. System.out.println("Binding to port " + port + ", please wait ...");
  36. chatField.setText(chatField.getText() + "\nBinding to port " + port + ", please wait ...");
  37. serverSocket = new ServerSocket(port);
  38. System.out.println("serverSocket started: " + serverSocket);
  39. chatField.setText(chatField.getText() + "\nserverSocket started: " + serverSocket);
  40. start();
  41. } catch (IOException ioe) {
  42. chatField.setText(chatField.getText() + ("Can not bind to port " + port + ": " + ioe.getMessage()));
  43. System.out.println("Can not bind to port " + port + ": " + ioe.getMessage());
  44. }
  45.  
  46. }
  47.  
  48. public void run() {
  49. while (t != null) {
  50. try {
  51. System.out.println("Waiting for a client ...");
  52. chatField.setText(chatField.getText() + "\nWaiting for a client ...");
  53. addThread(serverSocket.accept());
  54. } catch (IOException ioe) {
  55. System.out.println("serverSocket accept error: " + ioe);
  56. stop();
  57. }
  58. }
  59. }
  60.  
  61. public void start() {
  62. if (t == null) {
  63. t = new Thread(this);
  64. t.start();
  65. }
  66. }
  67.  
  68. public void stop() {
  69. if (t != null) {
  70. t.stop();
  71. t = null;
  72. }
  73. }
  74.  
  75. private int findClient(int ID) {
  76. for (int i = 0; i < noOfClients; i++)
  77. if (clients[i].getID() == ID)
  78. return i;
  79. return -1;
  80. }
  81.  
  82. public synchronized void handle(int ID, String input) {
  83. ChatServerThread client = clients[findClient(ID)];
  84. String userName = null, password = null, message = null;
  85. if (input.contains(".register")) {
  86. System.out.println("Registering " + input);
  87. String parsedInfo[] = input.split(Pattern.quote(" "));
  88. userName = parsedInfo[1];
  89. password = parsedInfo[2];
  90. registerUser(userName, password);
  91. } else if (input.contains(".login")) {
  92. String parsedInfo[] = input.split(Pattern.quote(" "));
  93. userName = parsedInfo[1];
  94. password = parsedInfo[2];
  95. if (checkLogin(userName, password) == false) {
  96. System.out.println("Telling user " + userName + " to register");
  97. client.send(".register");
  98. }
  99. client.send("SERVER: Login successful, welcome " + userName);
  100. } else if (input.charAt(0) != '.') {
  101. System.out.println(input);
  102. String parsedInfo[] = input.split(Pattern.quote("☼"));
  103. userName = parsedInfo[0];
  104. password = parsedInfo[1];
  105. message = parsedInfo[2];
  106. if (message.equals(".bye")) {
  107. client.send("Disconnecting now ...");
  108. client.send(".bye");
  109. remove(ID);
  110. } else if (message.contains("fuck")) {
  111. client.send("Rude! Timeout!\nDisconnecting now ...");
  112. client.send(".bye");
  113. remove(ID);
  114. } else if (message.length() > 160) {
  115. client.send("Client " + ID + " - too prolix!!!\nDisconnecting now ...");
  116. client.send(".bye");
  117. remove(ID);
  118. } else {
  119. for (int i = 0; i < noOfClients; i++) {
  120. chatField.setText(chatField.getText() + "\n" + userName + ": " + message);
  121. clients[i].send(userName + ": " + message);
  122. System.out.println("Sending message");
  123. }
  124. }
  125. }
  126.  
  127. }
  128.  
  129. public void registerUser(String username, String password) {
  130. try (FileWriter fw = new FileWriter("user_pass.txt", true);
  131. BufferedWriter bw = new BufferedWriter(fw);
  132. PrintWriter out = new PrintWriter(bw)) {
  133. out.println(username + ":" + password);
  134. } catch (IOException e) {
  135. // File writing/opening failed at some stage.
  136. }
  137. }
  138.  
  139. public boolean checkLogin(String username, String password) {
  140. boolean valid = false;
  141.  
  142. ArrayList<String> lines = new ArrayList<String>();
  143. String line;
  144. try {
  145. BufferedReader reader = new BufferedReader(new FileReader("user_pass.txt"));
  146. while ((line = reader.readLine()) != null) {
  147. lines.add(line);
  148. }
  149. } catch (Exception e) {
  150. e.printStackTrace();
  151. }
  152.  
  153. String[] users = new String[lines.size()];
  154. String[] pass = new String[lines.size()];
  155.  
  156. for (int i = 0; i < lines.size(); i++) {
  157. String[] output = lines.get(i).split("\\:");
  158. users[i] = output[0];
  159. pass[i] = output[1];
  160. }
  161.  
  162. for (int i = 0; i < lines.size(); i++) {
  163. System.out.println(users[i] + ":" + pass[i]);
  164. if (users[i].equals(username) && pass[i].equals(password)) {
  165. System.out.println("found a match");
  166. valid = true;
  167. break;
  168. }
  169. }
  170.  
  171. return valid;
  172. }
  173.  
  174. public synchronized void remove(int ID) {
  175. int pos = findClient(ID);
  176. if (pos >= 0) {
  177. ChatServerThread toTerminate = clients[pos];
  178. System.out.println("Removing client " + ID + " at " + pos);
  179. chatField.setText(chatField.getText() + "\nRemoving client " + ID + " at " + pos);
  180. if (pos < noOfClients - 1)
  181. for (int i = pos + 1; i < noOfClients; i++)
  182. clients[i - 1] = clients[i];
  183. noOfClients--;
  184. try {
  185. toTerminate.close();
  186. } catch (IOException ioe) {
  187. System.out.println("Error closing t: " + ioe);
  188. }
  189. toTerminate.stop();
  190. }
  191. }
  192.  
  193. private void addThread(Socket socket) {
  194. if (noOfClients < clients.length) {
  195. System.out.println("Client accepted: " + socket);
  196. chatField.setText(chatField.getText() + "\nClient accepted: " + socket);
  197. clients[noOfClients] = new ChatServerThread(this, socket);
  198. try {
  199. clients[noOfClients].open();
  200. clients[noOfClients].start();
  201. noOfClients++;
  202. } catch (IOException ioe) {
  203. System.out.println("Error opening thread: " + ioe);
  204. }
  205. } else {
  206. chatField.setText(chatField.getText() + "\nClient refused: maximum " + clients.length + " reached.");
  207. System.out.println("Client refused: maximum " + clients.length + " reached.");
  208. }
  209.  
  210. }
  211.  
  212. public static void main(String args[]) {
  213. ChatServer serverSocket = null;
  214. if (args.length != 1)
  215. System.out.println("Usage: java ChatServer port");
  216. else {
  217. java.awt.EventQueue.invokeLater(new Runnable() {
  218. public void run() {
  219. ChatServer serverSocket = new ChatServer(Integer.parseInt(args[0]));
  220. }
  221. });
  222. }
  223.  
  224. }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement