diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 8d0b776..30b486c 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -2,24 +2,26 @@ 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 String connectionFailedMessage; private Socket connectionToServer; + private BufferedReader fromServerReader; + private PrintWriter toServerWriter; // GUI private JTextArea outputTextArea; private JTextField inputTextField; + private JScrollPane outputScrollPane; - public ChatClient(int port) { + public ChatClient() { super("Chat"); - this.port = port; address = JOptionPane.showInputDialog("bitte IP-Adresse"); - - if (address != null) { + if (null != address) { + connectionFailedMessage = "Verbindung zum Server " + (address.isEmpty() ? "" : ("\"" + address + "\"")) + " fehlgeschlagen."; receiveMessages(); } } @@ -29,23 +31,56 @@ public class ChatClient extends JFrame implements KeyListener { outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + outputScrollPane = new JScrollPane(outputTextArea); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); - add(outputTextArea, BorderLayout.CENTER); + add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.SOUTH); setVisible(true); - setSize(800, 600); + setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT); setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); } private void receiveMessages() { try { - connectionToServer = new Socket(address, port); + connectionToServer = new Socket(address, Constants.PORT); + fromServerReader = new BufferedReader(new InputStreamReader(connectionToServer.getInputStream())); + toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); + initGui(); - } catch (Exception e) { - JOptionPane.showMessageDialog(null, "Verbindung zum Server \"" + address + "\" fehlgeschlagen."); + + while (true) { + String message = fromServerReader.readLine(); + outputTextArea.append(message + "\n"); + outputScrollPane.getVerticalScrollBar().setValue(outputScrollPane.getVerticalScrollBar().getMaximum()); + } + } catch (IOException e) { + JOptionPane.showMessageDialog(null, connectionFailedMessage); + dispose(); + } finally { + if (null != connectionToServer) { + try { + connectionToServer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if (null != fromServerReader) { + try { + fromServerReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if(null != toServerWriter) { + toServerWriter.close(); + } } } @@ -59,7 +94,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(""); } } @@ -71,6 +107,6 @@ public class ChatClient extends JFrame implements KeyListener { } public static void main(String[] args) { - new ChatClient(3141); + new ChatClient(); } } diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java new file mode 100644 index 0000000..5848138 --- /dev/null +++ b/src/main/java/Constants.java @@ -0,0 +1,5 @@ +public class Constants { + public static final int WINDOW_WIDTH = 800; + public static final int WINDOW_HEIGHT = 600; + public static final int PORT = 3141; +}