Client feritg, exit funktioniert und Verbindungsabbruch etc. geklärt. Start Button hinzugefügt, welcher auch den Usernamen unveränderlich macht. Time zu den Messages als Attribut hinzugefügt.

This commit is contained in:
BerdanInformatik123 2021-05-15 16:47:52 +02:00
parent e752709cdb
commit 70dded9ca2
3 changed files with 179 additions and 65 deletions

View file

@ -19,6 +19,9 @@ import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner; import java.util.Scanner;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -26,7 +29,7 @@ public class Client1 extends JFrame {
private static JPanel contentPane; private static JPanel contentPane;
JTextField txtMessage; JTextField txtMessage;
private JTextField txtUsername; private static JTextField txtUsername;
public static String currentText = " "; public static String currentText = " ";
public static int i = 0; public static int i = 0;
public static Object tosend = new Message("Leer", "Leer"); public static Object tosend = new Message("Leer", "Leer");
@ -34,8 +37,9 @@ public class Client1 extends JFrame {
public static Socket socket; public static Socket socket;
public static ObjectInputStream obinstr; public static ObjectInputStream obinstr;
public static ObjectOutputStream oboust; 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) { public static void main(String[] args) {
t1 = new Client1(); t1 = new Client1();
@ -73,13 +77,13 @@ public class Client1 extends JFrame {
txtMessage = new JTextField(); txtMessage = new JTextField();
txtMessage.setText("Message"); txtMessage.setText("Message");
txtMessage.setBounds(117, 29, 218, 20); txtMessage.setBounds(20, 230, 218, 20);
contentPane.add(txtMessage); contentPane.add(txtMessage);
txtMessage.setColumns(10); txtMessage.setColumns(10);
txtUsername = new JTextField(); txtUsername = new JTextField();
txtUsername.setText("Username"); txtUsername.setText("Username");
txtUsername.setBounds(21, 29, 86, 20); txtUsername.setBounds(20, 11, 86, 20);
contentPane.add(txtUsername); contentPane.add(txtUsername);
txtUsername.setColumns(10); txtUsername.setColumns(10);
@ -87,69 +91,171 @@ public class Client1 extends JFrame {
btnSend.addActionListener(new ActionListener() { btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
if(j != 0){
String themessage = txtMessage.getText(); String themessage = txtMessage.getText();
String theusername = txtUsername.getText(); String theusername = txtUsername.getText();
tosend = new Message(theusername, themessage); tosend = new Message(theusername, themessage);
i = 1; i = 1;
Message temp = new Message(theusername, themessage);
System.out.println("Button pressed"); System.out.println("Button pressed");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(socket.isConnected());
try{
try { try {
oboust.writeObject(tosend); oboust.writeObject(tosend);
oboust.flush(); oboust.flush();
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // 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); contentPane.add(btnSend);
JTextArea textArea = new JTextArea(currentText); // HIER PUBLIC GEMACHT
textArea = new JTextArea(currentText);
textArea.setLineWrap(true); textArea.setLineWrap(true);
textArea.setForeground(Color.WHITE); textArea.setForeground(Color.WHITE);
textArea.setBackground(Color.BLACK); textArea.setBackground(Color.LIGHT_GRAY);
textArea.setBounds(20, 60, 323, 176); textArea.setBounds(20, 42, 323, 176);
contentPane.add(textArea); 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){ public static void print(JTextArea textArea, String asd){
currentText = asd + "\n" + currentText; currentText = asd + "\n" + currentText;
textArea.setText(currentText); textArea.setText(currentText);
//System.out.println("ES FUNKTIONIERT");
} }
public static void start() throws IOException, ClassNotFoundException{ 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); Scanner scan = new Scanner(System.in);
JTextArea textArea = new JTextArea(currentText); //HIER PUBLIC GEMACHT
textArea = new JTextArea(currentText);
textArea.setLineWrap(true); textArea.setLineWrap(true);
textArea.setForeground(Color.WHITE); textArea.setForeground(Color.BLACK);
textArea.setBackground(Color.BLACK); textArea.setBackground(Color.LIGHT_GRAY);
textArea.setBounds(20, 60, 323, 176); textArea.setBounds(20, 42, 323, 176); //Dopplung
contentPane.add(textArea); contentPane.add(textArea);
socket = new Socket( "localhost", 1236 );
try{
socket = new Socket( "localhost", 1238 );
oboust = new ObjectOutputStream(socket.getOutputStream()); oboust = new ObjectOutputStream(socket.getOutputStream());
obinstr = new ObjectInputStream(socket.getInputStream()); 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; Object erhalten = null;
Message message = new Message("leer", "leer"); Message message = new Message("leer", "leer");
while(!message.getMessage().equals("exit")){ while(!message.getMessage().equals("exit")){
System.out.println("WHILE");
try{ try{
//System.out.println("hile4");
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
message = (Message)erhalten; message = (Message)erhalten;
String Ausgeben = "[" + message.getUsername() + "] " + message.getMessage();
String Ausgeben = message.getTime() + " [" + message.getUsername() + "] " + message.getMessage();
print(textArea, Ausgeben); print(textArea, Ausgeben);
//System.out.println("[CLIENT] NACHRICHT ERHALTEN");
oboust.flush(); oboust.flush();
}catch(Exception f){
}
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 { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -157,12 +263,15 @@ public class Client1 extends JFrame {
} }
} }
oboust.close(); oboust.close();
obinstr.close(); obinstr.close();
socket.close(); socket.close();
System.exit(0);
} }
} }

View file

@ -50,17 +50,6 @@ public class ServerHandelClientsThread extends Thread {
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
System.err.println(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) { } catch (SocketException ex) {
System.err.println("Socket geschlossen"); System.err.println("Socket geschlossen");
centralMessageStore.removeClient(this); centralMessageStore.removeClient(this);

View file

@ -1,29 +1,39 @@
package utils; package utils;
import java.io.Serializable; import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle; import java.time.format.FormatStyle;
import java.util.Date;
public class Message implements Serializable { public class Message implements Serializable {
String username; String username;
String message; String message;
//String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); String time = "00:00";
//LocalDateTime current = LocalDateTime.now(); /*
//DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); LocalDateTime current = LocalDateTime.now();
//String date = current.format(formatter); DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String date = current.format(formatter);
*/
public Message(String username, String message) { public Message(String username, String message) {
this.username = username; this.username = username;
this.message = message; this.message = message;
this.time = new SimpleDateFormat("HH.mm.ss").format(new Date());
//this.date = date; //this.date = date;
} }
/*
public String getMessage(){
return this.message;
}*/
@Override @Override
public String toString() { public String toString() {
return "nickname=" + username return "nickname=" + username
+ ", message=" + message; + ", message=" + message
+ ", time =" + time;
//+ ", time=" + date; //+ ", time=" + date;
} }
@ -42,4 +52,10 @@ public class Message implements Serializable {
return username; return username;
} }
public String getTime(){
return time;
}
} }