import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.*; import java.net.*; public class JClient extends JFrame implements ActionListener { public static final String NL = System.getProperty( "line.separator" ); public static final int DEFAULT_PORT = 80; private JTextField host = new JTextField(); private JTextArea input = new JTextArea( 20, 100 ); private JTextArea console = new JTextArea( 20, 100 ); private JButton submit = new JButton( "Submit" ); private JClient() { // Controllers setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); submit.addActionListener( this ); // Presentation setTitle( "JClient" ); host.setText( "www.site.uottawa.ca" ); input.setBorder( BorderFactory.createLineBorder( Color.black ) ); input.setFont( new Font( "Monospaced", Font.BOLD, 12 ) ); input.setBackground( Color.white ); console.setEditable( false ); console.setFont( new Font( "Monospaced", Font.BOLD, 12 ) ); console.setBackground( Color.white ); console.setForeground( Color.red ); log( "Info: Enter the HOST name at the top, an HTTP request in the second box, then click submit." ); JPanel center = new JPanel(); center.setLayout( new GridLayout( 2, 1 ) ); center.add( new JScrollPane( input ) ); center.add( new JScrollPane( console ) ); Container content = getContentPane(); content.add( submit, BorderLayout.SOUTH ); content.add( host, BorderLayout.NORTH ); content.add( center, BorderLayout.CENTER ); pack(); } private void log( String msg ) { console.append( msg + NL ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource() == submit ) { submit(); } } public void submit() { Socket socket = null; PrintWriter out = null; BufferedReader in = null; String name = host.getText(); boolean okay = false; log( "Info: Connecting to host: " + name ); try { socket = new Socket( name, DEFAULT_PORT ); out = new PrintWriter( socket.getOutputStream(), true ); in = new BufferedReader(new InputStreamReader( socket.getInputStream() ) ); okay = true; } catch ( UnknownHostException e ) { log( "Info: Unknown host: " + name ); } catch ( IOException e ) { log( "Info: Couldn't get I/O for the connection to: " + name ); } if ( ! okay ) { try { if ( in != null ) { in.close(); } if ( out != null ) { out.close(); } if ( socket != null ) { socket.close(); } } catch ( IOException e ) { log( "Info: Caught IOException" ); } } else { String fromServer; String fromUser; String[] lines = input.getText().split( NL ); for ( String line : lines ) { out.println( line ); } out.println( ); try { while ( (fromServer = in.readLine() ) != null) { log( "Server: " + fromServer ); } } catch ( IOException e ) { log( "Info: Caught IOException" ); } try { if ( in != null ) { in.close(); } if ( out != null ) { out.close(); } if ( socket != null ) { socket.close(); } } catch ( IOException e ) { log( "Info: Caught IOException" ); } log( "Info: Connection closed" ); } } public static void main( String[] args ) { JFrame app = new JClient(); app.setVisible( true ); } }