Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.net.SocketException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Date;
  9.  
  10. final class ChatServer {
  11. private static int uniqueId = 0;
  12. private final List<ClientThread> clients = new ArrayList<>();
  13. private final int port;
  14. private File filter;
  15.  
  16.  
  17. private ChatServer(int port, String filter) {
  18. this.port = port;
  19. this.filter = new File(filter);
  20. }
  21.  
  22. /*
  23. * This is what starts the ChatServer.
  24. * Right now it just creates the socketServer and adds a new ClientThread to a list to be handled
  25. */
  26. private void start() {
  27. try {
  28. ServerSocket serverSocket = new ServerSocket(port);
  29. while(true) {
  30. Socket socket = serverSocket.accept();
  31. Runnable r = new ClientThread(socket, uniqueId++);
  32. Thread t = new Thread(r);
  33. clients.add((ClientThread) r);
  34. t.start();
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. /*
  42. * > java ChatServer
  43. * > java ChatServer portNumber
  44. * If the port number is not specified 1500 is used
  45. */
  46. public static void main(String[] args) {
  47. int x = 1500;
  48. if(args.length != 0) {
  49. if (args[0] != null) {
  50. x = Integer.parseInt(args[0]);
  51. }
  52. }
  53. ChatServer server = new ChatServer(x, "src/badwords.txt");
  54. server.start();
  55. }
  56.  
  57.  
  58. /*
  59. * This is a private class inside of the ChatServer
  60. * A new thread will be created to run this every time a new client connects.
  61. */
  62. private final class ClientThread implements Runnable {
  63. Socket socket;
  64. ObjectInputStream sInput;
  65. ObjectOutputStream sOutput;
  66. int id;
  67. String username;
  68. ChatMessage cm;
  69.  
  70. private ClientThread(Socket socket, int id) {
  71. this.id = id;
  72. this.socket = socket;
  73. try {
  74. sOutput = new ObjectOutputStream(socket.getOutputStream());
  75. sInput = new ObjectInputStream(socket.getInputStream());
  76. username = (String) sInput.readObject();
  77.  
  78. } catch (IOException | ClassNotFoundException e ) {
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83. /*
  84. * This is what the client thread actually runs.
  85. */
  86. @Override
  87. public void run() {
  88. // Read the username sent to you by client
  89. while(true) {
  90. try {
  91. cm = (ChatMessage) sInput.readObject();
  92. int x = cm.getType();
  93. if (x == 1) {
  94. broadcast(username + " has logged out!");
  95. remove(this.id);
  96. sOutput.close();
  97. sInput.close();
  98. socket.close();
  99. }
  100. else {
  101. ChatFilter filter = new ChatFilter("src/badwords.txt");
  102. System.out.println(filter.filter(cm.getMessage()));
  103. broadcast(username + ": " + filter.filter(cm.getMessage()));
  104. }
  105.  
  106. }
  107. catch (SocketException e){
  108. }
  109. catch (EOFException e){
  110. }
  111. catch (IOException | ClassNotFoundException e) {
  112. }
  113.  
  114.  
  115. // Send message back to the client
  116. // try {
  117. // sOutput.writeObject("Pong");
  118. // } catch (IOException e) {
  119. // e.printStackTrace();
  120. // }
  121. }
  122. }
  123. private synchronized void remove(int x){
  124. clients.remove(x);
  125. }
  126. private boolean writeMessage(String msg){
  127. if(socket.isConnected()){
  128. try {
  129. sOutput.writeObject(msg);
  130. }
  131. catch(IOException e){
  132. }
  133. return true;
  134. }
  135. else{
  136. return false;
  137. }
  138. }
  139. private synchronized void broadcast(String message){
  140. for(int i = 0; i < clients.size();i++){
  141.  
  142. SimpleDateFormat date = new SimpleDateFormat("HH:mm:ss");
  143. clients.get(i).writeMessage(date.format(new Date()) + " " + message + "\n");
  144. System.out.println(date.format(new Date()) + " " + message);
  145.  
  146. }
  147.  
  148. }
  149. }
  150. }
  151. import java.io.EOFException;
  152. import java.io.IOException;
  153. import java.io.ObjectInputStream;
  154. import java.io.ObjectOutputStream;
  155. import java.net.Socket;
  156. import java.net.SocketException;
  157. import java.util.Scanner;
  158.  
  159. final class ChatClient {
  160. private ObjectInputStream sInput;
  161. private ObjectOutputStream sOutput;
  162. private Socket socket;
  163.  
  164. private final String server;
  165. private final String username;
  166. private final int port;
  167.  
  168. private ChatClient(String server, int port, String username) {
  169. this.server = server;
  170. this.port = port;
  171. this.username = username;
  172. }
  173.  
  174. /*
  175. * This starts the Chat Client
  176. */
  177. private boolean start() {
  178. // Create a socket
  179.  
  180. try {
  181. socket = new Socket(server, port);
  182. } catch (IOException e) {
  183. e.printStackTrace();
  184. }
  185.  
  186. // Create your input and output streams
  187. try {
  188. sInput = new ObjectInputStream(socket.getInputStream());
  189. sOutput = new ObjectOutputStream(socket.getOutputStream());
  190. } catch (IOException e) {
  191. e.printStackTrace();
  192. }
  193.  
  194. // This thread will listen from the server for incoming messages
  195. Runnable r = new ListenFromServer();
  196. Thread t = new Thread(r);
  197. t.start();
  198.  
  199. // After starting, send the clients username to the server.
  200. try {
  201. sOutput.writeObject(username);
  202. } catch (IOException e) {
  203. e.printStackTrace();
  204. }
  205.  
  206. return true;
  207. }
  208.  
  209.  
  210. /*
  211. * This method is used to send a ChatMessage Objects to the server
  212. */
  213. private void sendMessage(ChatMessage msg) {
  214. try {
  215. sOutput.writeObject(msg);
  216. } catch (IOException e) {
  217. e.printStackTrace();
  218. }
  219. }
  220.  
  221.  
  222. /*
  223. * To start the Client use one of the following command
  224. * > java ChatClient
  225. * > java ChatClient username
  226. * > java ChatClient username portNumber
  227. * > java ChatClient username portNumber serverAddress
  228. *
  229. * If the portNumber is not specified 1500 should be used
  230. * If the serverAddress is not specified "localHost" should be used
  231. * If the username is not specified "Anonymous" should be used
  232. */
  233. public static void main(String[] args) {
  234.  
  235. // Get proper arguments and override defaults
  236. String x = "";
  237. int y = 0;
  238. String z = "";
  239. for(int i = 0; i < 3; i++){
  240. if(i == 0){
  241. if( args.length == 1) {
  242. z = args[0];
  243. }
  244. else{
  245. z = "localhost";
  246. }
  247. }
  248. if(i == 1){
  249. if( args.length == 2) {
  250. y = Integer.parseInt(args[1]);
  251. }
  252. else{
  253. y = 1500;
  254. }
  255. }
  256. if(i == 2){
  257. if( args.length == 3) {
  258.  
  259. x = args[2];
  260.  
  261. }
  262. else{
  263. x = "Anonymous";
  264. }
  265. }
  266. }
  267. // Create your client and start it
  268. ChatClient client = new ChatClient(z, y, x);
  269.  
  270. client.start();
  271. // Send an empty message to the server
  272. Scanner scanner = new Scanner(System.in);
  273. boolean d = true;
  274. while(d ) {
  275. String msg = scanner.nextLine();
  276. if (msg.contains("/logout")) {
  277. client.sendMessage(new ChatMessage(1, msg));
  278. d = false;
  279. } else {
  280. client.sendMessage(new ChatMessage(0, msg));
  281. }
  282. }
  283. }
  284.  
  285.  
  286. /*
  287. * This is a private class inside of the ChatClient
  288. * It will be responsible for listening for messages from the ChatServer.
  289. * ie: When other clients send messages, the server will relay it to the client.
  290. */
  291. private final class ListenFromServer implements Runnable {
  292. public void run() {
  293. try {
  294. while(socket.isConnected()) {
  295. // if (!socket.isClosed()) {
  296. String msg = (String) sInput.readObject();
  297. System.out.print(msg);
  298. // }
  299. }
  300. }
  301. catch(EOFException e){
  302. }
  303. catch (IOException | ClassNotFoundException e) {
  304.  
  305. }
  306. }
  307. }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement