|  import 
                          javax.swing.*;import java.awt.*;
 import java.awt.event.*;
 import java.io.*;
 import java.net.*;
 
 /**
 * This program provides a graphical user 
                          interface to
 * set up the connection to a server and 
                          to download
 * the file from the server and display it 
                          in a text area.
 **/
 public class ClientApp extends JFrame
 implements Runnable, 
                          ActionListener
 {
 
 // Menu item names
 JMenuItem fMenuSave  = null;
 JMenuItem fMenuClose = null;
 
 //
 JTextArea  fTextArea = null;
 
 JTextField fIpField;
 JTextField fPortField;
 JTextField fFileField;
 
 // Default address is local
 String fIpAddr = "127.0.0.1";
 
 // Default port
 int fPort = 2222;
 
 // Socket to connect with server.
 Socket fSocket = null;
 
 // File name
 String fFilename = "message.html";
 
 // Change the button name as needed
 String fButtonName = "Get";
 JButton fButton = null;
 
 // Filter and File object for file chooser
 HtmlFilter fHtmlFilter = new HtmlFilter 
                          ();
 File fFile = new File ("default.html");
 
 Thread fThread = null;
 
 /** Start the program. **/
 public static void main (String [] args) 
                          {
 ClientApp f =
 new ClientApp ("Client 
                          for MicroServer ");
 f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 f.setVisible(true);
 }
 
 /**
 * Create user interface for 
                          client. Includes text fields
 * for the server IP address, 
                          the server's port number, a
 * text area to show the data 
                          returned from the server, and
 * a button to initiate the connection 
                          to the server.
 **/
 ClientApp (String title){
 super (title);
 
 Container content_pane = getContentPane 
                          ();
 
 // Create a user interface.
 content_pane.setLayout ( new 
                          BorderLayout () );
 fTextArea = new JTextArea ("");
 fTextArea.setLineWrap (true);
 
 content_pane.add ( fTextArea, 
                          "Center");
 
 // Create a panel with three 
                          text fields to obtain
 // the server address, the port 
                          number,
 // the file to download, and 
                          to initiate the link
 //  with the server.
 fIpField = new JTextField (fIpAddr);
 fPortField = new JTextField 
                          (""+fPort);
 fFileField= new JTextField (fFilename);
 
 // Button to initiate the download 
                          from the server
 fButton = new JButton (fButtonName);
 fButton.addActionListener (this);
 
 JPanel panel = new JPanel (new 
                          GridLayout (2,2));
 panel.add (fIpField);
 panel.add (fPortField);
 panel.add (fFileField);
 panel.add (fButton);
 
 content_pane.add ( panel, "South");
 
 // Use the helper method makeMenuItem
 // for making the menu items 
                          and registering
 // their listener.
 JMenu m = new JMenu ("File");
 
 // File handling.
 m.add (fMenuSave  = 
                          makeMenuItem ("Save"));
 m.add (fMenuClose = makeMenuItem 
                          ("Quit"));
 
 JMenuBar mb = new JMenuBar ();
 mb.add (m);
 
 setJMenuBar (mb);
 setSize (400,400);
 
 setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 } // ctor
 
 /** Process events from the frame menu and 
                          the chooser.**/
 public void actionPerformed ( ActionEvent 
                          e ){
 boolean status = false;
 String command = e.getActionCommand 
                          ();
 
 if (command.equals (fButtonName) 
                          ) {
 
 // First get the 
                          server address
 fIpAddr = fIpField.getText 
                          ();
 
 // Get port number
 fPort = Integer.parseInt 
                          (fPortField.getText ());
 
 // Get the name 
                          of the file to download
 fFilename = fFileField.getText 
                          ();
 
 start ();
 
 } else if (command.equals ("Save") 
                          ) {
 // Save a file
 status = saveFile 
                          ();
 if ( !status)
 JOptionPane.showMessageDialog 
                          (
 null,
 "IO 
                          error in saving file!!", "File Save Error",
 JOptionPane.ERROR_MESSAGE);
 
 
 } else if (command.equals ("Quit") 
                          ){
 dispose ();
 }
 } // actionPerformed
 
 /**
 * Start the thread to connect to the server
 * and read the file from it.
 */
 public void start (){
 
 // If the thread reference not 
                          null then a
 // thread is already running. 
                          Otherwise, create
 // a thread and start it.
 if (fThread == null) {
 fThread 
                          = new Thread (this);
 fThread.start 
                          ();
 }
 } // start
 
 /** Connect with the server via a thread 
                          process. **/
 public void run () {
 
 // Clear the text area
 fTextArea.setText ("Downloading...");
 
 try{
 // Connect to the 
                          server via the given IP address and port number
 fSocket = new Socket 
                          (fIpAddr, fPort);
 
 // Assemble the 
                          message line.
 String message = 
                          "GET /" + fFilename;
 
 // Now get an output 
                          stream to the server.
 OutputStream server_out 
                          = fSocket.getOutputStream ();
 
 // Wrap in writer 
                          classes
 PrintWriter pw_server_out 
                          = new PrintWriter (
 new 
                          OutputStreamWriter (server_out, "8859_1"), true );
 
 // Send the message 
                          to the server
 pw_server_out.println 
                          (message);
 
 // Get the input 
                          stream from the server and then
 // wrap the stream 
                          in two wrappers.
 BufferedReader server_reader 
                          = new BufferedReader (
 new 
                          InputStreamReader ( fSocket.getInputStream () )  );
 
 fTextArea.setText 
                          ("");
 String line;
 // Add the text 
                          one line at a time from the server
 // to the text area.
 while  ((line 
                          = server_reader.readLine ()) != null )
 fTextArea.append 
                          (line + '\n');
 
 } catch  ( UnknownHostException 
                          uhe) {
 fTextArea.setText 
                          ("Unknown host");
 
 } catch  ( IOException 
                          ioe){
 fTextArea.setText 
                          ("I/O Exception");
 } finally{
 try{
 // End 
                          the connection
 fSocket.close 
                          ();
 fThread=null;
 } catch ( IOException 
                          ioe) {
 fTextArea.append 
                          ("IO Exception while closing socket");
 }
 }
 } // run
 
 /**
 * This "helper method" makes 
                          a menu item and then
 * registers this object as a 
                          listener to it.
 **/
 private JMenuItem makeMenuItem (String name) 
                          {
 JMenuItem m = new JMenuItem 
                          ( name );
 m.addActionListener ( this );
 return m;
 }
 
 /**
 * Use a JFileChooser in Save 
                          mode to select files
 * to open. Use a filter for 
                          FileFilter subclass to select
 * for *.java files. If a file 
                          is selected, then write the
 * the string from the text area 
                          into it.
 **/
 boolean saveFile () {
 
 File file = null;
 JFileChooser fc = new JFileChooser 
                          ();
 
 // Start in current directory
 fc.setCurrentDirectory (new 
                          File ("."));
 
 // Set filter for web pages.
 fc.setFileFilter (fHtmlFilter);
 
 // Set to a default name for 
                          save.
 fc.setSelectedFile (file);
 
 // Open chooser dialog
 int result = fc.showSaveDialog 
                          (this);
 
 if (result == JFileChooser.CANCEL_OPTION) 
                          {
 return true;
 }else if (result == JFileChooser.APPROVE_OPTION) 
                          {
 file 
                          = fc.getSelectedFile ();
 if (file.exists 
                          ()) {
 int 
                          response = JOptionPane.showConfirmDialog (null,
 "Overwrite 
                          existing file?","Confirm Overwrite",
 JOptionPane.OK_CANCEL_OPTION,
 JOptionPane.QUESTION_MESSAGE);
 if 
                          (response == JOptionPane.CANCEL_OPTION) return false;
 }
 return 
                          writeFile (file, fTextArea.getText ());
 } else  {
 return 
                          false;
 }
 } // saveFile
 
 /**
 * Use a PrintWriter wrapped 
                          around a BufferedWriter, which in turn
 * is wrapped around a FileWriter, 
                          to write the string data to the
 * given file.
 **/
 public static boolean writeFile (File file, 
                          String data_string){
 
 try {
 PrintWriter 
                          out =
 new PrintWriter (new BufferedWriter (new FileWriter 
                          (file)));
 out.print 
                          (data_string);
 out.flush 
                          ();
 out.close 
                          ();
 }catch  (IOException 
                          e) {
 return 
                          false;
 }
 return true;
 }
 } // class ClientApp
 
 
 /** Class to use with JFileChooser for selecting hypertext 
                          files.**/
 class HtmlFilter extends javax.swing.filechooser.FileFilter{
 
 public boolean accept (File f){
 return f.getName ().toLowerCase 
                          ().endsWith (".htm")
 || f.getName 
                          ().toLowerCase ().endsWith (".html")
 || f.isDirectory 
                          ();
 }
 
 public String getDescription (){
 return "Web pages  (*.htm*)";
 }
 } // class HtmlFilter
 
 
 |