2021-05-13 17:40:22 +00:00
|
|
|
package client;
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
import java.awt.BorderLayout;
|
|
|
|
import java.awt.EventQueue;
|
|
|
|
import java.awt.TextArea;
|
2021-05-13 17:40:22 +00:00
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.border.EmptyBorder;
|
2021-05-13 17:40:22 +00:00
|
|
|
|
2021-05-13 19:09:10 +00:00
|
|
|
import utils.Message;
|
2021-05-13 17:40:22 +00:00
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
import javax.swing.JTextField;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JTextArea;
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.net.Socket;
|
2021-05-15 14:47:52 +00:00
|
|
|
import java.net.SocketException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2021-05-15 08:45:47 +00:00
|
|
|
import java.util.Scanner;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
public class Client1 extends JFrame {
|
|
|
|
|
|
|
|
private static JPanel contentPane;
|
|
|
|
JTextField txtMessage;
|
2021-05-15 14:47:52 +00:00
|
|
|
private static JTextField txtUsername;
|
2021-05-15 08:45:47 +00:00
|
|
|
public static String currentText = " ";
|
|
|
|
public static int i = 0;
|
|
|
|
public static Object tosend = new Message("Leer", "Leer");
|
|
|
|
public static Client1 t1;
|
|
|
|
public static Socket socket;
|
|
|
|
public static ObjectInputStream obinstr;
|
|
|
|
public static ObjectOutputStream oboust;
|
2021-05-15 14:47:52 +00:00
|
|
|
public static int j = 0;
|
|
|
|
public static JTextArea textArea = new JTextArea();
|
|
|
|
public static int k = 0;
|
2021-05-15 08:45:47 +00:00
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
t1 = new Client1();
|
|
|
|
EventQueue.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Client1 frame = new Client1();
|
|
|
|
frame.setVisible(true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2021-05-15 14:47:52 +00:00
|
|
|
}
|
2021-05-15 08:45:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
start();
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Client1() {
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
setBounds(100, 100, 450, 300);
|
|
|
|
contentPane = new JPanel();
|
|
|
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
|
|
setContentPane(contentPane);
|
|
|
|
contentPane.setLayout(null);
|
|
|
|
|
|
|
|
txtMessage = new JTextField();
|
|
|
|
txtMessage.setText("Message");
|
2021-05-15 14:47:52 +00:00
|
|
|
txtMessage.setBounds(20, 230, 218, 20);
|
2021-05-15 08:45:47 +00:00
|
|
|
contentPane.add(txtMessage);
|
|
|
|
txtMessage.setColumns(10);
|
|
|
|
|
|
|
|
txtUsername = new JTextField();
|
|
|
|
txtUsername.setText("Username");
|
2021-05-15 14:47:52 +00:00
|
|
|
txtUsername.setBounds(20, 11, 86, 20);
|
2021-05-15 08:45:47 +00:00
|
|
|
contentPane.add(txtUsername);
|
|
|
|
txtUsername.setColumns(10);
|
|
|
|
|
|
|
|
JButton btnSend = new JButton("Send");
|
|
|
|
btnSend.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
if(j != 0){
|
2021-05-15 08:45:47 +00:00
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
String themessage = txtMessage.getText();
|
|
|
|
String theusername = txtUsername.getText();
|
2021-05-15 08:45:47 +00:00
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
tosend = new Message(theusername, themessage);
|
|
|
|
i = 1;
|
2021-05-15 08:45:47 +00:00
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
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) {
|
2021-05-15 08:45:47 +00:00
|
|
|
// TODO Auto-generated catch block
|
2021-05-15 14:47:52 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}} catch( Exception k){
|
|
|
|
print(textArea, "KEINE VERBINDUNG");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(temp.getMessage().equals("exit")){
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
2021-05-13 17:40:22 +00:00
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
btnSend.setBounds(264, 229, 79, 23);
|
2021-05-15 08:45:47 +00:00
|
|
|
contentPane.add(btnSend);
|
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
// HIER PUBLIC GEMACHT
|
|
|
|
textArea = new JTextArea(currentText);
|
2021-05-15 08:45:47 +00:00
|
|
|
textArea.setLineWrap(true);
|
|
|
|
textArea.setForeground(Color.WHITE);
|
2021-05-15 14:47:52 +00:00
|
|
|
textArea.setBackground(Color.LIGHT_GRAY);
|
|
|
|
textArea.setBounds(20, 42, 323, 176);
|
2021-05-15 08:45:47 +00:00
|
|
|
contentPane.add(textArea);
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
public static void print(JTextArea textArea, String asd){
|
|
|
|
currentText = asd + "\n" + currentText;
|
2021-05-15 14:47:52 +00:00
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
textArea.setText(currentText);
|
2021-05-15 14:47:52 +00:00
|
|
|
//System.out.println("ES FUNKTIONIERT");
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void start() throws IOException, ClassNotFoundException{
|
2021-05-15 14:47:52 +00:00
|
|
|
//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");
|
2021-05-15 08:45:47 +00:00
|
|
|
Scanner scan = new Scanner(System.in);
|
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
//HIER PUBLIC GEMACHT
|
|
|
|
textArea = new JTextArea(currentText);
|
2021-05-15 08:45:47 +00:00
|
|
|
textArea.setLineWrap(true);
|
2021-05-15 14:47:52 +00:00
|
|
|
textArea.setForeground(Color.BLACK);
|
|
|
|
textArea.setBackground(Color.LIGHT_GRAY);
|
|
|
|
textArea.setBounds(20, 42, 323, 176); //Dopplung
|
2021-05-15 08:45:47 +00:00
|
|
|
contentPane.add(textArea);
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
2021-05-16 07:18:15 +00:00
|
|
|
socket = new Socket( "localhost", 1236 );
|
2021-05-15 14:47:52 +00:00
|
|
|
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);
|
2021-05-15 08:45:47 +00:00
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
}
|
2021-05-15 08:45:47 +00:00
|
|
|
Object erhalten = null;
|
|
|
|
Message message = new Message("leer", "leer");
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
|
|
|
|
while(!message.getMessage().equals("exit")){
|
2021-05-15 14:47:52 +00:00
|
|
|
System.out.println("WHILE");
|
2021-05-15 08:45:47 +00:00
|
|
|
try{
|
2021-05-15 14:47:52 +00:00
|
|
|
//System.out.println("hile4");
|
2021-05-15 20:53:58 +00:00
|
|
|
System.out.println("Nachricht erhalten");
|
|
|
|
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
|
2021-05-15 08:45:47 +00:00
|
|
|
|
2021-05-15 14:47:52 +00:00
|
|
|
message = (Message)erhalten;
|
2021-05-15 20:53:58 +00:00
|
|
|
System.out.println("[" + message.getUsername() + "] " + message.getMessage());
|
2021-05-15 14:47:52 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
oboust.close();
|
|
|
|
obinstr.close();
|
|
|
|
socket.close();
|
2021-05-15 14:47:52 +00:00
|
|
|
|
|
|
|
System.exit(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-15 08:45:47 +00:00
|
|
|
}
|
|
|
|
}
|