diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8d0b776..ce66879 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,13 +2,14 @@ import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import java.io.IOException; +import java.io.*; import java.net.Socket; public class ChatClient extends JFrame implements KeyListener { private String address; private int port; private Socket connectionToServer; + private PrintWriter toServerWriter; // GUI private JTextArea outputTextArea; @@ -43,8 +44,9 @@ public class ChatClient extends JFrame implements KeyListener { private void receiveMessages() { try { connectionToServer = new Socket(address, port); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); - } catch (Exception e) { + } catch (IOException e) { JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); } } @@ -59,7 +61,8 @@ public class ChatClient extends JFrame implements KeyListener { if (e.getKeyCode() == KeyEvent.VK_ENTER) { String message = inputTextField.getText(); if (!message.isEmpty()) { - // Senden die Nachricht an Server, wenn der erstellt wird + toServerWriter.println(message); + toServerWriter.flush(); inputTextField.setText(""); } }