From 59ed34c94a581efc7dc7caf4512e75a05d763a2e Mon Sep 17 00:00:00 2001 From: BerdanInformatik123 Date: Sat, 15 May 2021 10:45:47 +0200 Subject: [PATCH] Client1 funktioniert. Message modifiziert. --- src/client/Client1.java | 191 ++++++++++++++++++++++++++++++++-------- src/utils/Message.java | 15 ++++ 2 files changed, 170 insertions(+), 36 deletions(-) diff --git a/src/client/Client1.java b/src/client/Client1.java index 5502290..4184fd2 100644 --- a/src/client/Client1.java +++ b/src/client/Client1.java @@ -1,49 +1,168 @@ package client; -/** - * @author berdan - */ +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; import utils.Message; -import java.net.*; -import java.io.*; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; + +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; +import java.util.Scanner; +import java.awt.event.ActionEvent; + +public class Client1 extends JFrame { + + private static JPanel contentPane; + JTextField txtMessage; + private JTextField txtUsername; + 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; + + + + 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(); + } + } + }); + + + 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"); + txtMessage.setBounds(117, 29, 218, 20); + contentPane.add(txtMessage); + txtMessage.setColumns(10); + + txtUsername = new JTextField(); + txtUsername.setText("Username"); + txtUsername.setBounds(21, 29, 86, 20); + contentPane.add(txtUsername); + txtUsername.setColumns(10); + + JButton btnSend = new JButton("Send"); + btnSend.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + + String themessage = txtMessage.getText(); + String theusername = txtUsername.getText(); + + tosend = new Message(theusername, themessage); + i = 1; + + System.out.println("Button pressed"); + + try { + oboust.writeObject(tosend); + oboust.flush(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } -public class Client1 { - public Client1() throws IOException, ClassNotFoundException { - Socket socket = new Socket( "localhost", 1236 ); - ObjectOutputStream oboust = new ObjectOutputStream(socket.getOutputStream()); - ObjectInputStream obinstr = new ObjectInputStream(socket.getInputStream()); + } + }); + + btnSend.setBounds(345, 28, 79, 23); + contentPane.add(btnSend); + + JTextArea textArea = new JTextArea(currentText); + textArea.setLineWrap(true); + textArea.setForeground(Color.WHITE); + textArea.setBackground(Color.BLACK); + textArea.setBounds(20, 60, 323, 176); + contentPane.add(textArea); + } + + public static void print(JTextArea textArea, String asd){ + currentText = asd + "\n" + currentText; + textArea.setText(currentText); + } + + public static void start() throws IOException, ClassNotFoundException{ + Scanner scan = new Scanner(System.in); + + JTextArea textArea = new JTextArea(currentText); + textArea.setLineWrap(true); + textArea.setForeground(Color.WHITE); + textArea.setBackground(Color.BLACK); + textArea.setBounds(20, 60, 323, 176); + contentPane.add(textArea); - Message senden = new Message( "berdan", "test" ); - System.out.println(senden); + socket = new Socket( "localhost", 1236 ); + oboust = new ObjectOutputStream(socket.getOutputStream()); + obinstr = new ObjectInputStream(socket.getInputStream()); + + Object erhalten = null; + Message message = new Message("leer", "leer"); + + while(!message.getMessage().equals("exit")){ + try{ + erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt - // Message senden - oboust.writeObject(senden); - oboust.flush(); - - // Message erhalten - - Object erhalten = null; - while(erhalten==null){ - erhalten = obinstr.readObject(); - } - System.out.println(erhalten); + message = (Message)erhalten; + String Ausgeben = "[" + message.getUsername() + "] " + message.getMessage(); + print(textArea, Ausgeben); + oboust.flush(); + }catch(Exception f){ + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } - oboust.close(); - obinstr.close(); - } + } + oboust.close(); + obinstr.close(); + socket.close(); + } + + - public static void main(String args[]) throws IOException, ClassNotFoundException { - new Client1(); - } + } - - diff --git a/src/utils/Message.java b/src/utils/Message.java index 85d32cc..bc3c1a3 100644 --- a/src/utils/Message.java +++ b/src/utils/Message.java @@ -26,5 +26,20 @@ public class Message implements Serializable { + ", message=" + message; //+ ", time=" + date; } + + public boolean exit() { + if(message.equalsIgnoreCase("exit")){ + return false; + } + return true; + } + + public String getMessage() { + return message; + } + + public String getUsername() { + return username; + } }