From 6f39b1ba46d4449ac74eb90637fea9417fb1f983 Mon Sep 17 00:00:00 2001 From: eichehome Date: Fri, 14 May 2021 17:44:48 +0200 Subject: [PATCH] Nachrichtenversenden gefixt/Test-Klassen in eigenes Paket --- src/client/MainClient.java | 65 ----------------------- src/server/ClientMessageStore.java | 7 ++- src/server/ServerHandelClientsThread.java | 25 ++++++--- src/test/MainClient.java | 43 +++++++++++++++ src/{server => test}/PipeTest.java | 6 +-- src/utils/ArrayHelper.java | 9 +++- src/utils/Message.java | 10 ++-- 7 files changed, 81 insertions(+), 84 deletions(-) delete mode 100644 src/client/MainClient.java create mode 100644 src/test/MainClient.java rename src/{server => test}/PipeTest.java (97%) diff --git a/src/client/MainClient.java b/src/client/MainClient.java deleted file mode 100644 index b719911..0000000 --- a/src/client/MainClient.java +++ /dev/null @@ -1,65 +0,0 @@ - -package client; - -import java.io.*; -import java.net.*; - - -public class MainClient { - - static Socket server = null; - static PrintWriter pr = null; - static BufferedReader bf = null; - static InputStreamReader in = null; - - /** - * @param args the command line arguments - */ - public static void main(String[] args) { - try { - server = new Socket("localhost", 1236); - - pr = new PrintWriter(server.getOutputStream()); - pr.println("is it working?"); - pr.flush(); - - in = new InputStreamReader(server.getInputStream()); - bf = new BufferedReader(in); - String str = bf.readLine(); - System.out.println("server: " + str); - try { - System.out.println("Sleepin for 5 seconds"); - Thread.sleep(10000); - } catch (InterruptedException e) { - System.out.println("Thread is interrupted"); - } - } catch (IOException e) { - System.err.println("Fehler: " + e); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - System.err.println("Fehler: " + e); - } - } - if (bf != null) { - try { - bf.close(); - } catch (IOException e) { - System.err.println("Fehler: " + e); - } - } - if (pr != null) { - pr.close(); - } - if (server != null) { - try { - server.close(); - } catch (IOException e) { - System.err.println("Fehler: " + e); - } - } - } - } -} diff --git a/src/server/ClientMessageStore.java b/src/server/ClientMessageStore.java index 4835439..50956ff 100644 --- a/src/server/ClientMessageStore.java +++ b/src/server/ClientMessageStore.java @@ -17,7 +17,6 @@ package server; import utils.FifoPipe; -import java.net.Socket; import utils.ArrayHelper; import utils.Message; @@ -53,4 +52,10 @@ public class ClientMessageStore { public synchronized Thread removeThread(Thread thread) { return arrayHelper.popThread(thread, clientThreads); } + + public void notifyAllClients() { + for (int i = 0; i < clientThreads.length; i++) { + clientThreads[i].interrupt(); + } + } } diff --git a/src/server/ServerHandelClientsThread.java b/src/server/ServerHandelClientsThread.java index be38dfe..ad8c95e 100644 --- a/src/server/ServerHandelClientsThread.java +++ b/src/server/ServerHandelClientsThread.java @@ -17,9 +17,10 @@ package server; import java.io.*; -import java.net.*; import java.util.logging.Level; import java.util.logging.Logger; +import java.net.*; +import utils.Message; /** * @@ -35,19 +36,27 @@ public class ServerHandelClientsThread extends Thread { public void run() { boolean continueLoop = true; while (continueLoop) { - try (InputStreamReader in = new InputStreamReader(clientSocket.getInputStream())) { - pr = new PrintWriter(clientSocket.getOutputStream()); - BufferedReader bf = new BufferedReader(in); - for(String clientInput = bf.readLine(); clientInput != null; clientInput = bf.readLine()) { - System.out.println("client: " + clientInput); - pr.write("yes"); - pr.flush(); + try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream()); + ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) { + Message empfangen = null; + while ((empfangen = (Message) in.readObject()) != null) { + System.out.println("client: " + empfangen); + Message message = empfangen; + out.writeObject(message); + out.flush(); } + } catch (ClassNotFoundException ex) { + System.err.println(ex); + //for(String clientInput = in.readLine(); clientInput != null; clientInput = in.readLine()) { + //} } catch (InterruptedException ex) { System.err.println("test gegkückt"); System.err.println(ex); pr.write("Test"); Thread.currentThread().interrupt(); + } catch (SocketException ex) { + System.err.println("Socket geschlossen"); + continueLoop = false; } catch (IOException ex) { System.err.println("Exeption: " + ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); diff --git a/src/test/MainClient.java b/src/test/MainClient.java new file mode 100644 index 0000000..14ed231 --- /dev/null +++ b/src/test/MainClient.java @@ -0,0 +1,43 @@ + +package test; + +import java.io.*; +import java.net.*; +import utils.Message; + + +public class MainClient { + + public MainClient() throws IOException, ClassNotFoundException { + System.out.println("[CLIENT] START"); + Socket socket = new Socket( "localhost", 1236 ); + ObjectOutputStream oboust = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream obinstr = new ObjectInputStream(socket.getInputStream()); + System.out.println("[CLIENT] START1"); + Message senden = new Message( "berdan", "test" ); + System.out.println(senden); //nickname=berdan, message=test + + // Message senden + oboust.writeObject(senden); + oboust.flush(); + System.out.println("[CLIENT] NACHRICHT GESENDET"); + + // Message erhalten + + Object erhalten = null; + + while(erhalten==null){ + erhalten = obinstr.readObject(); + } + System.out.println("[CLIENT] NACHRICHT ERHALTEN"); + System.out.println(erhalten); //nickname=berdan, message=test + + oboust.close(); + obinstr.close(); + socket.close(); + } + + public static void main(String args[]) throws IOException, ClassNotFoundException { + new MainClient(); + } +} diff --git a/src/server/PipeTest.java b/src/test/PipeTest.java similarity index 97% rename from src/server/PipeTest.java rename to src/test/PipeTest.java index bae1bac..a4c2c43 100644 --- a/src/server/PipeTest.java +++ b/src/test/PipeTest.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package server; +package test; import utils.ArrayHelper; import java.io.IOException; @@ -31,7 +31,7 @@ import utils.PipeElement; public class PipeTest { public static void main(String[] args) { - FifoPipe pipe = new FifoPipe(); + /*FifoPipe pipe = new FifoPipe(); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); Message el = new Message("Christian", "Test"); @@ -68,7 +68,7 @@ public class PipeTest { System.out.println("Letztes Element: " + pipe.lastElement); ArrayHelper test = new ArrayHelper(); - + */ } } diff --git a/src/utils/ArrayHelper.java b/src/utils/ArrayHelper.java index 86168fd..26e0cb5 100644 --- a/src/utils/ArrayHelper.java +++ b/src/utils/ArrayHelper.java @@ -19,6 +19,7 @@ package utils; /** * * @author eichehome + * @param Type des Arrays, mit dem gearbeitet wird. */ public class ArrayHelper { @@ -42,7 +43,8 @@ public class ArrayHelper { if (array != null) { int index = getIndex(element, array); T result = array[index]; - T[] temp = new T[2 - array.length]; + T[] temp; + temp = new T[array.length - 2]; for (int i = 0; i < index; i++) { temp[i] = array[i]; } @@ -56,10 +58,13 @@ public class ArrayHelper { } int getIndex(T match, T[] array) { + int result = -1; for (int i = 0; i < array.length; i++) { if (array[i].equals(match)) { - return i; + result = i; + break; } } + return result; } } diff --git a/src/utils/Message.java b/src/utils/Message.java index c32f71e..85d32cc 100644 --- a/src/utils/Message.java +++ b/src/utils/Message.java @@ -10,9 +10,9 @@ public class Message implements Serializable { String username; String message; //String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); - LocalDateTime current = LocalDateTime.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); - String date = current.format(formatter); + //LocalDateTime current = LocalDateTime.now(); + //DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); + //String date = current.format(formatter); public Message(String username, String message) { this.username = username; @@ -23,8 +23,8 @@ public class Message implements Serializable { @Override public String toString() { return "nickname=" + username - + ", message=" + message - + ", time=" + date; + + ", message=" + message; + //+ ", time=" + date; } }