commit d2d48e8eab92a41f81edc824017f7e774f0f7c51 Author: Timo Geier Date: Tue Nov 21 07:52:32 2017 +0100 First working version diff --git a/Online-Chat-Dev.iml b/Online-Chat-Dev.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/Online-Chat-Dev.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/com/vertsys/Client.java b/src/com/vertsys/Client.java new file mode 100644 index 0000000..156ee26 --- /dev/null +++ b/src/com/vertsys/Client.java @@ -0,0 +1,68 @@ +package com.vertsys; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.Socket; +import java.util.Random; + +public class Client { + + protected static Socket sock; + private static String username; + private static BufferedReader ConsoleIn; + + public static void main(String[] args) { + init(); + do { + try { + String msg = ConsoleIn.readLine(); + sendMessage(msg); + } catch (IOException e) { + e.printStackTrace(); + } + }while (true); + } + + public static void init(){ + String hostname = "localhost"; + int port = 42424; + + // Falls die Vergabe eines Benutzernamen fehlschlägt --> "test" + Zufallszahl + Random rand = new Random(); + username = "test" + Integer.toString(rand.nextInt(4)); + try { + ConsoleIn = new BufferedReader(new InputStreamReader(System.in)); + System.out.print("Your Username: "); + String user = ConsoleIn.readLine(); + if (user != "") + username = user; + System.out.print("Enter Server hostname: "); + hostname = ConsoleIn.readLine(); + System.out.print("Enter port number: "); + port = Integer.parseInt(ConsoleIn.readLine()); + }catch (IOException e){ + e.printStackTrace(); + } + try { + sock = new Socket(hostname, port); + System.out.println("Connection established!"); + System.out.println("--------------------------------"); + System.out.println(""); + new Thread(new ClientThread()).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void sendMessage(String message){ + String moddedMessage = username + " wrote: " + message + "\n"; + try { + DataOutputStream toServer = new DataOutputStream(sock.getOutputStream()); + toServer.writeBytes(moddedMessage); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/com/vertsys/ClientThread.java b/src/com/vertsys/ClientThread.java new file mode 100644 index 0000000..28b1bcb --- /dev/null +++ b/src/com/vertsys/ClientThread.java @@ -0,0 +1,41 @@ +package com.vertsys; + +//import static com.vertsys.Server.sock; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.util.ArrayList; + + +public class ClientThread extends Client implements Runnable{ + + @Override + public void run() { + ArrayList msgs = new ArrayList<>(); + try { + ObjectInputStream allMessagesStream = new ObjectInputStream(sock.getInputStream()); + msgs = (ArrayList) allMessagesStream.readObject(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + + msgs.forEach(msg -> { + System.out.println(msg); + }); + + while(!sock.isClosed()){ + try { + BufferedReader fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream())); + String newMessage = fromServer.readLine(); + System.out.println(newMessage); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/com/vertsys/Server.java b/src/com/vertsys/Server.java new file mode 100644 index 0000000..54657e9 --- /dev/null +++ b/src/com/vertsys/Server.java @@ -0,0 +1,65 @@ +package com.vertsys; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; + + +public class Server { + protected static ServerSocket sock; + protected static ArrayList messages = new ArrayList<>(); + protected static ArrayList clients = new ArrayList<>(); +// protected static ArrayList messages = new ArrayList<>(); + protected static int port; + + protected static boolean DEBUG_MODE = true; + + public static void main(String[] args){ + init(args); + try { + sock = new ServerSocket(port); + } catch (IOException e) { + e.printStackTrace(); + } + while (true){ + if (DEBUG_MODE) + System.out.println("Socket opened... Waiting for connections"); + try { + Socket connSocket = sock.accept(); + clients.add(connSocket); + new Thread(new ServerThread(connSocket)).start(); + } catch (IOException e){ + + } + } + + } + + + public static void init(String[] args){ + + switch (args.length) { + case 0: + if (DEBUG_MODE) + System.out.println("No Parameters. Set port to default values..."); + port = 42424; + break; + case 1: + if (DEBUG_MODE) + System.out.println("Port as Parameter."); + port = Integer.parseInt(args[1]); + if (port < 0) { + System.err.println("ERROR: Port must be a value >= 0!"); + System.out.println("Usage: java ConcatServer [port]"); + System.exit(-1); + } + break; + + default: + System.out.println("Usage: java ConcatServer [port]"); + System.exit(0); + break; + } + } +} diff --git a/src/com/vertsys/ServerThread.java b/src/com/vertsys/ServerThread.java new file mode 100644 index 0000000..aea1eb6 --- /dev/null +++ b/src/com/vertsys/ServerThread.java @@ -0,0 +1,63 @@ +package com.vertsys; + +import java.io.*; +import java.net.Socket; + +public class ServerThread extends Server implements Runnable{ + + private Socket sock; + + public ServerThread(Socket sock){ + this.sock = sock; + } + + @Override + public void run() { + if (DEBUG_MODE) + System.out.println("New connection --> New Thread started."); + if(DEBUG_MODE) + System.out.println("Send all received messages to new client..."); + try { + ObjectOutputStream toNewClient = new ObjectOutputStream(sock.getOutputStream()); + toNewClient.writeObject(messages); + } catch (IOException e) { + e.printStackTrace(); + } + while(!sock.isClosed()){ + System.out.println("Still running..."); + this.addMessage(); + } + if(DEBUG_MODE) + System.out.println("Socket closed --> Joining Thread..."); + } + + private void addMessage(){ + String newMessage = ""; + try { + newMessage = readMessage(); + } catch (IOException e) { + e.printStackTrace(); + } + if (DEBUG_MODE) + System.out.println("Add new Message..."); + messages.add(newMessage); + if(DEBUG_MODE) + System.out.println(newMessage); + String finalNewMessage = newMessage; + clients.forEach(socket -> { + DataOutputStream outgoing = null; + + try { + outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeBytes(finalNewMessage + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + + }); + } + private String readMessage() throws IOException { + BufferedReader incoming = new BufferedReader(new InputStreamReader(sock.getInputStream())); + return incoming.readLine(); + } +}