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