Unnötiger Code entfernt

This commit is contained in:
BerdanInformatik123 2021-05-16 14:17:05 +02:00
parent 9169534afb
commit e1ef8a767d
1 changed files with 51 additions and 54 deletions

View File

@ -1,8 +1,6 @@
package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
@ -17,8 +15,6 @@ 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;
@ -46,9 +42,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 boolean verbunden = false;
public static JTextArea textArea = new JTextArea();
public static int k = 0;
public static int anzahlVersuche = 0; //Ist gleich die AN
public static int anzahlRekursionen = 0;
public static Message temp = new Message("leer", "leer");
/*
@ -56,7 +53,7 @@ public class Client1 extends JFrame {
start() Methode aufgerufen.
*/
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
t1 = new Client1();
EventQueue.invokeLater(new Runnable() {
public void run() {
@ -123,7 +120,7 @@ public class Client1 extends JFrame {
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(j != 0){
if(verbunden){
String themessage = txtMessage.getText();
String theusername = txtUsername.getText();
@ -132,20 +129,19 @@ public class Client1 extends JFrame {
i = 1;
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");
try {
oboust.writeObject(tosend);
oboust.flush();
} catch (IOException e) {
e.printStackTrace();
}
} catch( Exception k){
print("KEINE VERBINDUNG");
}
if(temp.getMessage().equals("exit")){
@ -168,7 +164,7 @@ public class Client1 extends JFrame {
*/
textArea = new JTextArea(currentText);
textArea.setLineWrap(true);
textArea.setForeground(Color.WHITE);
textArea.setForeground(Color.BLACK);
textArea.setBackground(Color.LIGHT_GRAY);
textArea.setBounds(20, 42, 323, 176);
contentPane.add(textArea);
@ -188,7 +184,7 @@ public class Client1 extends JFrame {
txtUsername.setEditable(false);
txtUsername.setEnabled(false);
j = 1;
verbunden = true;
}
});
@ -204,16 +200,16 @@ public class Client1 extends JFrame {
auf die textArea abgebildet.
*/
public static void print(JTextArea textArea, String neuerText){
public static void print(String neuerText){
currentText = neuerText + "\n" + currentText;
textArea.setText(currentText);
}
/*
Die start-Methode wartet, bis die Variable j durch Klicken des Start
Buttons auf 1 gesetzt wird, erst dann beginnt die eigentliche
Methode.
Die start-Methode wartet, bis die Variable verbunden durch Klicken
des Start Buttons auf true gesetzt wird, erst dann beginnt die
eigentliche Methode.
Es wird dann eine Verbindung zum Socket über den Port
1236 aufgebaut. Sollte dies nicht Möglich sein, wird die
@ -225,15 +221,10 @@ public class Client1 extends JFrame {
*/
public static void start() throws IOException, ClassNotFoundException{
public static void start() throws IOException, ClassNotFoundException, InterruptedException{
while(j==0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!verbunden){
Thread.sleep(100);
}
txtUsername.setEnabled(false);
@ -243,15 +234,21 @@ public class Client1 extends JFrame {
socket = new Socket( "localhost", 1236 );
oboust = new ObjectOutputStream(socket.getOutputStream());
obinstr = new ObjectInputStream(socket.getInputStream());
print(textArea, "VERBINDUNG HERGESTELLT");
print("VERBINDUNG HERGESTELLT");
}catch(Exception KeineSocket){
print(textArea, "SERVER ANTWORTET NICHT");
print("SERVER ANTWORTET NICHT");
try {
Thread.sleep(1000);
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
anzahlRekursionen++;
if(anzahlRekursionen == 10){
print("KEINE ANTWORT, CLIENT WIRD BEENDET");
Thread.sleep(10000);
System.exit(0);
}
start();
System.exit(0);
@ -288,7 +285,7 @@ public class Client1 extends JFrame {
message = (Message)erhalten;
System.out.println("[" + message.getUsername() + "] " + message.getMessage());
String Ausgeben = message.getTime() + " [" + message.getUsername() + "] " + message.getMessage();
print(textArea, Ausgeben);
print(Ausgeben);
//System.out.println("[CLIENT] NACHRICHT ERHALTEN");
oboust.flush();
@ -297,31 +294,31 @@ public class Client1 extends JFrame {
catch(SocketException h){
System.out.println(h);
print(textArea, "VERBINDUNGSABBRUCH");
k++;
print("VERBINDUNGSABBRUCH");
anzahlVersuche++;
Thread.sleep(2000);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(k ==5){
k = 0;
if(anzahlVersuche==5){
anzahlVersuche = 0;
anzahlRekursionen++;
if(anzahlRekursionen == 10){
print("KEINE ANTWORT, CLIENT WIRD BEENDET");
Thread.sleep(10000);
System.exit(0);
}
start();
System.exit(0);
}
}
catch(Exception f){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread.sleep(1000);
}
}