import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import java.net.*; /** * IP 4.1.1 * @author emi-lind * */ public class ChatClient extends JFrame { private String _host; private int _port; private Socket _socket; private BufferedReader _in; private PrintWriter _out; private JTextField _inputField; private JTextArea _outputArea; /** * @param args */ public static void main(String[] args) { new ChatClient(args); } /** * * @param aArgs The command line from main */ public ChatClient(String[] aArgs) { this.readCommandLineArgs(aArgs); this.createGUI(); _socket = this.initConnection(_host, _port); // If connection established if (_socket != null) { String connectionInfo = "ChatClient. Host: " + _socket.getInetAddress() + " Port: " + _socket.getPort(); this.setTitle(connectionInfo); try { this._in = new BufferedReader(new InputStreamReader(this._socket.getInputStream())); this._out = new PrintWriter(new OutputStreamWriter(this._socket.getOutputStream(), "ISO-8859-1"), true); } catch (IOException e) { System.err.println(e); } new ServerReader(); } else { //No connection, quit the program System.exit(1); } } /** * Reads the command line. * @param aArgs The command-line to read */ private void readCommandLineArgs(String[] aArgs) { String host = "127.0.0.1"; int port = 2000; switch (aArgs.length) { case 2: port = Integer.parseInt(aArgs[1]); // Fall-through case 1: host = aArgs[0]; break; } _host = host; _port = port; } private void createGUI() { this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(550, 600); _inputField = new JTextField(""); _outputArea = new JTextArea(""); _outputArea.setEditable(false); _inputField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String userInput = ChatClient.this._inputField.getText(); ChatClient.this._out.println(userInput); ChatClient.this._inputField.setText(""); } }); Container c = this.getContentPane(); c.add(_inputField, BorderLayout.SOUTH); JScrollPane outScroll = new JScrollPane(_outputArea); //Auto scroll outScroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { e.getAdjustable().setValue(e.getAdjustable().getMaximum()); } }); c.add(outScroll, BorderLayout.CENTER); this.validate(); } /** * Creates a connection to a given host/ip and port. * @param aHost The hosts address * @param aPort The port * @return A socket connected to given host. Null if no connection were made */ private Socket initConnection(String aHost, int aPort) { Socket toHost = null; try { toHost = new Socket(aHost, aPort); } catch(UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } return toHost; } private class ServerReader implements Runnable { public ServerReader() { Thread t = new Thread(this); t.start(); } @Override public void run() { while (true) { try { String readed = ChatClient.this._in.readLine(); ChatClient.this._outputArea.append(readed + "\n"); } catch (IOException e) { System.err.print(e); } } } } }