Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Date;
- final class ChatServer {
- private static int uniqueId = 0;
- private final List<ClientThread> clients = new ArrayList<>();
- private final int port;
- private File filter;
- private ChatServer(int port, String filter) {
- this.port = port;
- this.filter = new File(filter);
- }
- /*
- * This is what starts the ChatServer.
- * Right now it just creates the socketServer and adds a new ClientThread to a list to be handled
- */
- private void start() {
- try {
- ServerSocket serverSocket = new ServerSocket(port);
- while(true) {
- Socket socket = serverSocket.accept();
- Runnable r = new ClientThread(socket, uniqueId++);
- Thread t = new Thread(r);
- clients.add((ClientThread) r);
- t.start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*
- * > java ChatServer
- * > java ChatServer portNumber
- * If the port number is not specified 1500 is used
- */
- public static void main(String[] args) {
- int x = 1500;
- if(args.length != 0) {
- if (args[0] != null) {
- x = Integer.parseInt(args[0]);
- }
- }
- ChatServer server = new ChatServer(x, "src/badwords.txt");
- server.start();
- }
- /*
- * This is a private class inside of the ChatServer
- * A new thread will be created to run this every time a new client connects.
- */
- private final class ClientThread implements Runnable {
- Socket socket;
- ObjectInputStream sInput;
- ObjectOutputStream sOutput;
- int id;
- String username;
- ChatMessage cm;
- private ClientThread(Socket socket, int id) {
- this.id = id;
- this.socket = socket;
- try {
- sOutput = new ObjectOutputStream(socket.getOutputStream());
- sInput = new ObjectInputStream(socket.getInputStream());
- username = (String) sInput.readObject();
- } catch (IOException | ClassNotFoundException e ) {
- e.printStackTrace();
- }
- }
- /*
- * This is what the client thread actually runs.
- */
- @Override
- public void run() {
- // Read the username sent to you by client
- while(true) {
- try {
- cm = (ChatMessage) sInput.readObject();
- int x = cm.getType();
- if (x == 1) {
- broadcast(username + " has logged out!");
- remove(this.id);
- sOutput.close();
- sInput.close();
- socket.close();
- }
- else {
- ChatFilter filter = new ChatFilter("src/badwords.txt");
- System.out.println(filter.filter(cm.getMessage()));
- broadcast(username + ": " + filter.filter(cm.getMessage()));
- }
- }
- catch (SocketException e){
- }
- catch (EOFException e){
- }
- catch (IOException | ClassNotFoundException e) {
- }
- // Send message back to the client
- // try {
- // sOutput.writeObject("Pong");
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- }
- }
- private synchronized void remove(int x){
- clients.remove(x);
- }
- private boolean writeMessage(String msg){
- if(socket.isConnected()){
- try {
- sOutput.writeObject(msg);
- }
- catch(IOException e){
- }
- return true;
- }
- else{
- return false;
- }
- }
- private synchronized void broadcast(String message){
- for(int i = 0; i < clients.size();i++){
- SimpleDateFormat date = new SimpleDateFormat("HH:mm:ss");
- clients.get(i).writeMessage(date.format(new Date()) + " " + message + "\n");
- System.out.println(date.format(new Date()) + " " + message);
- }
- }
- }
- }
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.net.SocketException;
- import java.util.Scanner;
- final class ChatClient {
- private ObjectInputStream sInput;
- private ObjectOutputStream sOutput;
- private Socket socket;
- private final String server;
- private final String username;
- private final int port;
- private ChatClient(String server, int port, String username) {
- this.server = server;
- this.port = port;
- this.username = username;
- }
- /*
- * This starts the Chat Client
- */
- private boolean start() {
- // Create a socket
- try {
- socket = new Socket(server, port);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Create your input and output streams
- try {
- sInput = new ObjectInputStream(socket.getInputStream());
- sOutput = new ObjectOutputStream(socket.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- // This thread will listen from the server for incoming messages
- Runnable r = new ListenFromServer();
- Thread t = new Thread(r);
- t.start();
- // After starting, send the clients username to the server.
- try {
- sOutput.writeObject(username);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return true;
- }
- /*
- * This method is used to send a ChatMessage Objects to the server
- */
- private void sendMessage(ChatMessage msg) {
- try {
- sOutput.writeObject(msg);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*
- * To start the Client use one of the following command
- * > java ChatClient
- * > java ChatClient username
- * > java ChatClient username portNumber
- * > java ChatClient username portNumber serverAddress
- *
- * If the portNumber is not specified 1500 should be used
- * If the serverAddress is not specified "localHost" should be used
- * If the username is not specified "Anonymous" should be used
- */
- public static void main(String[] args) {
- // Get proper arguments and override defaults
- String x = "";
- int y = 0;
- String z = "";
- for(int i = 0; i < 3; i++){
- if(i == 0){
- if( args.length == 1) {
- z = args[0];
- }
- else{
- z = "localhost";
- }
- }
- if(i == 1){
- if( args.length == 2) {
- y = Integer.parseInt(args[1]);
- }
- else{
- y = 1500;
- }
- }
- if(i == 2){
- if( args.length == 3) {
- x = args[2];
- }
- else{
- x = "Anonymous";
- }
- }
- }
- // Create your client and start it
- ChatClient client = new ChatClient(z, y, x);
- client.start();
- // Send an empty message to the server
- Scanner scanner = new Scanner(System.in);
- boolean d = true;
- while(d ) {
- String msg = scanner.nextLine();
- if (msg.contains("/logout")) {
- client.sendMessage(new ChatMessage(1, msg));
- d = false;
- } else {
- client.sendMessage(new ChatMessage(0, msg));
- }
- }
- }
- /*
- * This is a private class inside of the ChatClient
- * It will be responsible for listening for messages from the ChatServer.
- * ie: When other clients send messages, the server will relay it to the client.
- */
- private final class ListenFromServer implements Runnable {
- public void run() {
- try {
- while(socket.isConnected()) {
- // if (!socket.isClosed()) {
- String msg = (String) sInput.readObject();
- System.out.print(msg);
- // }
- }
- }
- catch(EOFException e){
- }
- catch (IOException | ClassNotFoundException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement