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.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,8 +37,9 @@ 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();
@ -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,69 +91,171 @@ public class Client1 extends JFrame {
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(j != 0){
String themessage = txtMessage.getText();
String theusername = txtUsername.getText();
tosend = new Message(theusername, themessage);
i = 1;
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();
}} 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 );
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{
//System.out.println("hile4");
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
message = (Message)erhalten;
String Ausgeben = "[" + message.getUsername() + "] " + message.getMessage();
String Ausgeben = message.getTime() + " [" + message.getUsername() + "] " + message.getMessage();
print(textArea, Ausgeben);
//System.out.println("[CLIENT] NACHRICHT ERHALTEN");
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 {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@ -157,12 +263,15 @@ public class Client1 extends JFrame {
}
}
oboust.close();
obinstr.close();
socket.close();
System.exit(0);
}
}

View file

@ -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);

View file

@ -1,29 +1,39 @@
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;
}
@ -42,4 +52,10 @@ public class Message implements Serializable {
return username;
}
public String getTime(){
return time;
}
}