import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Client extends JPanel implements ActionListener { protected static JTextField textField; protected static JTextArea textArea; protected static JButton but; public static String nick; private static InetAddress ch; private static final int PORT = 7777; private static final String ERROR = "ERROR : "; private final static String newline = "\n"; public Scanner scan; public PrintWriter out; public static int new1 = 1; public static Socket l; public Scanner in; public static String mess; public static void main(String[] args) { try { ch = InetAddress.getLocalHost(); System.out.println("******************************************"); System.out.println("*------CONNECTED TO SERVER ON "+PORT+".------*"); System.out.println("******************************************"); } catch(IOException e) { System.out.println(ERROR + "Host not found!"); System.exit(1); } nick = JOptionPane.showInputDialog("Enter Your Nickname."); JFrame frame = new JFrame("Client GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new Client(); newContentPane.setOpaque(true); frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void run() { Socket l = null; try { l = new Socket("modarators.no-ip.org", PORT); Scanner scan = new Scanner(l.getInputStream()); PrintWriter out = new PrintWriter(l.getOutputStream() , true); Scanner in = new Scanner(System.in); String response; out.println(nick + "has loged in."); response = scan.nextLine(); GUIprint(response); response = ""; do { mess = textField.getText(); out.println(mess); response = scan.nextLine(); GUIprint(response); } while(!mess.equals("--CLOSE--")); } catch(IOException e) { e.printStackTrace(); } finally { try { System.out.println("\nClosing Connection.."); l.close(); } catch(IOException e) { System.out.println(ERROR + "Unable to dissconect!"); System.exit(1); } } } public static void GUIprint(String s) { String txt = s; if(!txt.equals("")) { textArea.append(nick + " : " + txt + newline); } } public Client() { super(new GridBagLayout()); textField = new JTextField(25); textField.addActionListener(this); but = new JButton("Send"); but.addActionListener(this); textArea = new JTextArea(10, 30); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea ,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; add(scrollPane, c); add(textField); add(but); } public void actionPerformed(ActionEvent e) { run(); textArea.setCaretPosition(textArea.getDocument().getLength()); } }