From 70dded9ca27bfd1ed7cc2848c9bc24b494516244 Mon Sep 17 00:00:00 2001 From: BerdanInformatik123 Date: Sat, 15 May 2021 16:47:52 +0200 Subject: [PATCH] =?UTF-8?q?Client=20feritg,=20exit=20funktioniert=20und=20?= =?UTF-8?q?Verbindungsabbruch=20etc.=20gekl=C3=A4rt.=20Start=20Button=20hi?= =?UTF-8?q?nzugef=C3=BCgt,=20welcher=20auch=20den=20Usernamen=20unver?= =?UTF-8?q?=C3=A4nderlich=20macht.=20Time=20zu=20den=20Messages=20als=20At?= =?UTF-8?q?tribut=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/Client1.java | 199 +++++++++++++++++----- src/server/ServerHandelClientsThread.java | 11 -- src/utils/Message.java | 34 +++- 3 files changed, 179 insertions(+), 65 deletions(-) diff --git a/src/client/Client1.java b/src/client/Client1.java index 4184fd2..03fbccd 100644 --- a/src/client/Client1.java +++ b/src/client/Client1.java @@ -19,6 +19,9 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; +import java.net.SocketException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Scanner; import java.awt.event.ActionEvent; @@ -26,7 +29,7 @@ public class Client1 extends JFrame { private static JPanel contentPane; JTextField txtMessage; - private JTextField txtUsername; + private static JTextField txtUsername; public static String currentText = " "; public static int i = 0; public static Object tosend = new Message("Leer", "Leer"); @@ -34,9 +37,10 @@ public class Client1 extends JFrame { public static Socket socket; public static ObjectInputStream obinstr; public static ObjectOutputStream oboust; + public static int j = 0; + public static JTextArea textArea = new JTextArea(); + public static int k = 0; - - public static void main(String[] args) { t1 = new Client1(); EventQueue.invokeLater(new Runnable() { @@ -47,7 +51,7 @@ public class Client1 extends JFrame { } catch (Exception e) { e.printStackTrace(); } - } + } }); @@ -73,13 +77,13 @@ public class Client1 extends JFrame { txtMessage = new JTextField(); txtMessage.setText("Message"); - txtMessage.setBounds(117, 29, 218, 20); + txtMessage.setBounds(20, 230, 218, 20); contentPane.add(txtMessage); txtMessage.setColumns(10); txtUsername = new JTextField(); txtUsername.setText("Username"); - txtUsername.setBounds(21, 29, 86, 20); + txtUsername.setBounds(20, 11, 86, 20); contentPane.add(txtUsername); txtUsername.setColumns(10); @@ -87,82 +91,187 @@ public class Client1 extends JFrame { btnSend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { - String themessage = txtMessage.getText(); - String theusername = txtUsername.getText(); + if(j != 0){ - tosend = new Message(theusername, themessage); - i = 1; + String themessage = txtMessage.getText(); + String theusername = txtUsername.getText(); - System.out.println("Button pressed"); + tosend = new Message(theusername, themessage); + i = 1; - try { - oboust.writeObject(tosend); - oboust.flush(); - } catch (IOException e) { + Message temp = new Message(theusername, themessage); + + System.out.println("Button pressed"); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + System.out.println(socket.isConnected()); + try{ + try { + oboust.writeObject(tosend); + oboust.flush(); + } catch (IOException e) { // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(); + }} catch( Exception k){ + print(textArea, "KEINE VERBINDUNG"); + } + + if(temp.getMessage().equals("exit")){ + System.exit(0); + } + + } } }); - btnSend.setBounds(345, 28, 79, 23); + btnSend.setBounds(264, 229, 79, 23); contentPane.add(btnSend); - JTextArea textArea = new JTextArea(currentText); + // HIER PUBLIC GEMACHT + textArea = new JTextArea(currentText); textArea.setLineWrap(true); textArea.setForeground(Color.WHITE); - textArea.setBackground(Color.BLACK); - textArea.setBounds(20, 60, 323, 176); + textArea.setBackground(Color.LIGHT_GRAY); + textArea.setBounds(20, 42, 323, 176); contentPane.add(textArea); + + JButton btnStart = new JButton("Start"); + btnStart.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + + txtUsername.setEditable(false); + txtUsername.setEnabled(false); + j = 1; + + } + }); + btnStart.setBounds(116, 10, 89, 23); + contentPane.add(btnStart); + + + } + + + + public static void print(JTextArea textArea, String asd){ currentText = asd + "\n" + currentText; + textArea.setText(currentText); + //System.out.println("ES FUNKTIONIERT"); + } public static void start() throws IOException, ClassNotFoundException{ + //JTextArea textArea = new JTextArea("BERDAN"); + + while(j==0){ + try { + Thread.sleep(10); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + txtUsername.setEnabled(false); + + System.out.println("NOTEDITABLE"); Scanner scan = new Scanner(System.in); - JTextArea textArea = new JTextArea(currentText); + //HIER PUBLIC GEMACHT + textArea = new JTextArea(currentText); textArea.setLineWrap(true); - textArea.setForeground(Color.WHITE); - textArea.setBackground(Color.BLACK); - textArea.setBounds(20, 60, 323, 176); + textArea.setForeground(Color.BLACK); + textArea.setBackground(Color.LIGHT_GRAY); + textArea.setBounds(20, 42, 323, 176); //Dopplung contentPane.add(textArea); - - socket = new Socket( "localhost", 1236 ); - oboust = new ObjectOutputStream(socket.getOutputStream()); - obinstr = new ObjectInputStream(socket.getInputStream()); + + + + try{ + socket = new Socket( "localhost", 1238 ); + oboust = new ObjectOutputStream(socket.getOutputStream()); + obinstr = new ObjectInputStream(socket.getInputStream()); + print(textArea, "VERBINDUNG HERGESTELLT"); + }catch(Exception KeineSocket){ + print(textArea, "SERVER ANTWORTET NICHT"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + start(); + System.exit(0); + } Object erhalten = null; Message message = new Message("leer", "leer"); + + + + while(!message.getMessage().equals("exit")){ + System.out.println("WHILE"); try{ - erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt + //System.out.println("hile4"); + erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt - message = (Message)erhalten; - String Ausgeben = "[" + message.getUsername() + "] " + message.getMessage(); - print(textArea, Ausgeben); - oboust.flush(); - }catch(Exception f){ - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + message = (Message)erhalten; + String Ausgeben = message.getTime() + " [" + message.getUsername() + "] " + message.getMessage(); + print(textArea, Ausgeben); + //System.out.println("[CLIENT] NACHRICHT ERHALTEN"); + + oboust.flush(); + + } + + catch(SocketException h){ + System.out.println(h); + print(textArea, "VERBINDUNGSABBRUCH"); + k++; + + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if(k ==5){ + k = 0; + start(); + System.exit(0); + } + + } + catch(Exception f){ + try { + Thread.sleep(1000); + + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + oboust.close(); obinstr.close(); socket.close(); + + System.exit(0); + + + } - - - - } diff --git a/src/server/ServerHandelClientsThread.java b/src/server/ServerHandelClientsThread.java index 68bafbe..ccd23c8 100644 --- a/src/server/ServerHandelClientsThread.java +++ b/src/server/ServerHandelClientsThread.java @@ -50,17 +50,6 @@ public class ServerHandelClientsThread extends Thread { } } catch (ClassNotFoundException ex) { System.err.println(ex); - } catch (InterruptedException ex) { - System.err.println("test gegkückt"); - System.err.println(ex); - Message message = pipe.getNextElement(); - try { - out.writeObject(message); - System.out.println("Nachricht gesendet"); - } catch (IOException ex) { - System.err.println("Fehler: " + ex); - } - Thread.currentThread().interrupt(); } catch (SocketException ex) { System.err.println("Socket geschlossen"); centralMessageStore.removeClient(this); diff --git a/src/utils/Message.java b/src/utils/Message.java index bc3c1a3..a9d8177 100644 --- a/src/utils/Message.java +++ b/src/utils/Message.java @@ -1,33 +1,43 @@ package utils; import java.io.Serializable; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; +import java.util.Date; 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); - + String time = "00:00"; + /* + LocalDateTime current = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); + String date = current.format(formatter); + */ public Message(String username, String message) { this.username = username; this.message = message; + this.time = new SimpleDateFormat("HH.mm.ss").format(new Date()); //this.date = date; } + + /* + public String getMessage(){ + return this.message; + }*/ @Override public String toString() { return "nickname=" + username - + ", message=" + message; + + ", message=" + message + + ", time =" + time; //+ ", time=" + date; } - - public boolean exit() { + + public boolean exit() { if(message.equalsIgnoreCase("exit")){ return false; } @@ -41,5 +51,11 @@ public class Message implements Serializable { public String getUsername() { return username; } + + public String getTime(){ + return time; + } + + -} +} \ No newline at end of file