Comp Sci Advanced Java: Client/Server Application Problem

Click For Summary
The Java Swing client/server application is designed to retrieve and display the contents of a specified text file from the server. The issue arises when the output is formatted, resulting in the file content being displayed in a single long line instead of separate lines. The use of the `output.format(result)` method in the server class is identified as the source of the problem, as it fails to format the output correctly for multiple lines. Suggestions for resolving the formatting issue include using `output.format(result + "\n")` or switching to a different output method that handles line breaks appropriately. The discussion emphasizes the need for proper formatting in socket communication to ensure the client receives the file content as intended.
iamjon.smith
Messages
117
Reaction score
3
Assignment:

Write a client and server, Java Swing application using socket connections that allows the client to specify a filename to the server in a Textfield and the server send the contents of the text file back to the client if it exists. If the file does not exist, then it should send back an appropriate error message instead.

Problem:

The program runs as expected, except for one small problem. I am having a formatting issue with the output. The connection to the server is acquired, the file is read, and output to the screen, but it prints to the screen in one long line. I tried to use the formatter that the scanner class (class I am using for input/output) "%s\n" which should write one line, drop down, write next line, but when I do this, it stops writing after the first line. If I remove the formatter, it writes the whole file. Take a look and give me some ideas please.

Code is as follows:
Client class
Code:
/*
 * @author      Jonathan Smith
 * Date:        April 8, 2011
 * Assignment:  Phase 1 DB 2
 * Course:      IT315-1102C-02
 * Instructor:  Michael Williamson
 * College:     CTU Online
 *
 * Purpose:     To read in a file from a server where the file name is dictated by the user.
 * If the file exists, display file in text area, else, display file not found error message
 *
 * Testing: Attempt to retrieve file that does not exist (error displays),
 * then retrieve existing file (display file contents in text area)
 */

package ClientServer;

// Client portion of a stream-socket connection between client and server.
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Client extends JFrame implements ActionListener
{
   private JTextField enterField; // text field to accept input from user
   private JTextArea displayArea; // display area to display information from user
   private Scanner  input; // input from user
   private Formatter output; // output to server
   private Socket connection; // connection to server
   private JPanel panel; // panel to hold components
   private JLabel label; // label to prompt user
   private JScrollPane scroller; // scroller for text area


   // set up GUI, connect to server, and get streams
   public Client()
   {
     label = new JLabel ( " Enter the FILE NAME to retrieve: ");
     panel = new JPanel(); // create panel
     panel.setLayout ( new GridLayout (1, 2, 0, 0));
     panel.add( label ); // add label to panel
     enterField = new JTextField(); // create text field
     enterField.addActionListener( this ); // add action listener
     panel.add( enterField ); // add text area to panel
     displayArea = new JTextArea(); // add text area
     scroller = new JScrollPane( displayArea ); // add scrolling
     setLayout( new BorderLayout() ); // set layout of JFrame
     add( panel, BorderLayout.NORTH); // add panel  to north
     add( scroller, BorderLayout.CENTER); // add scrolling text area to center

     try // connect to server, get streams
     {
         // create socket to make connection to server
         connection = new Socket( InetAddress.getLocalHost(), 7985 );
         // local host address and port identical to server port number
         output = new Formatter(connection.getOutputStream()); // output stream to server
         output.flush(); // flush output to server
         input = new Scanner( connection.getInputStream() ); // input stream from server
     } // end try
     catch( IOException ioexception)
     {
         ioexception.printStackTrace();
     } // end catch

      setSize(400, 200);  // set size of window
      setVisible( true ); // show window
   } // end client constructor


   // process file name entered by client
   public void actionPerformed(ActionEvent event)
    {
       try      // display contents of file
       {
           String fileName  = event.getActionCommand() + "\n";
           output.format( fileName ); // put file name in stream
           output.flush(); // send to server
           String inputLine = input.nextLine(); // read response from server
           displayArea.setText( inputLine ); // show response line in text area

           // if file  exists,  display file contents
           if (inputLine.equals( "The file contains:"))
           {
               while ( input.hasNextLine() ); // read new line
               {
                   inputLine = input.nextLine(); // read response from server
                   displayArea.append(inputLine + "\n"); // add line to text area
               } // end while
           } // end if

           enterField.setEditable( true ); // enable editing of enterfield
           enterField.setBackground( Color.red ); // set color
       } // end try
       finally
       {
           try
           {
             input.close();       // close input
             output.close();      // close output
             connection.close();  // close connection
           } // end try
           catch (IOException ioexception)
           {
               ioexception.printStackTrace();
               System.exit(1);
           } // end catch

       } // end finally
    } // end Client class
} // end class Client

Server Class:

Code:
/*
 * @author      Jonathan Smith
 * Date:        April 8, 2011
 * Assignment:  Phase 1 DB 2
 * Course:      IT315-1102C-02
 * Instructor:  Michael Williamson
 * College:     CTU Online
 *
 * Purpose:     To read in a file from a server where the file name is dictated by the user.
 * If the file exists, display file in text area, else, display file not found error message
 *
 * Testing: Attempt to retrieve file that does not exist (error displays),
 * then retrieve existing file (display file contents in text area)
 */

package ClientServer;

// Server portion of a client/server stream-socket connection.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;
import java.io.FileReader;
import java.util.Formatter;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Server extends JFrame
{

   private Formatter output; // output stream to client
   private Scanner input; // input stream from client
   private ServerSocket server; // server socket
   private Socket connection; // connection to client

   // set up GUI
   public Server()
   {
       try
       {
            server = new ServerSocket( 7985, 10 ); // create ServerSocket
            // first parameter is port
            // second is how many clients server can queue
       } // end try
       catch (IOException exception)
       {
            exception.printStackTrace();
            System.exit(0);
       } // end catch

   } // end Server constructor

   // set up and run server
   public void runServer()
   {
      try // set up server to receive connections; process connections
      {
         connection = server.accept();
         output = new Formatter(connection.getOutputStream()); // output stream for socket
         output.flush();                                       // flush output stream
         input = new Scanner(connection.getInputStream());     // input stream for socket
         File inputFile = new File(input.nextLine());          // get file name from client
         String result;                                        // result variable for

         if ( inputFile.exists())
         {
             Scanner fileInput = new Scanner( inputFile );
             output.format("The file contains:" );
             output.flush();                    // send to client

             while (fileInput.hasNextLine() )   // while there are more lines in the file
             {
                 result = fileInput.nextLine(); // read a line from the file
                 output.format( result ); // output line of file  **PROBLEM HERE**
                 output.flush();                // send to client
             } // end while
         } // end if
         else
         {
             result = inputFile.getName() + " does not exist! \n";
             output.format( result );       // inform client the file does not exist
             output.flush();                //send to client
         } // end else
      } // end try
      catch (IOException ioException)
      {
         ioException.printStackTrace();
         System.exit(0);
      } //  end catch
      finally
      {
          try
          {
              output.close();       // close output
              input.close();        // close input
              connection.close();   //close connection
          } // end try
          catch (IOException ioException)
          {
            ioException.printStackTrace();
            System.exit(0);
          } //  end catch
      } // end finally
    } // end runServer method
}

Client Test Class:

Code:
/*
 * @author      Jonathan Smith
 * Date:        April 8, 2011
 * Assignment:  Phase 1 DB 2
 * Course:      IT315-1102C-02
 * Instructor:  Michael Williamson
 * College:     CTU Online
 *
 * Purpose:     To read in a file from a server where the file name is dictated by the user.
 * If the file exists, display file in text area, else, display file not found error message
 *
 * Testing: Attempt to retrieve file that does not exist (error displays),
 * then retrieve existing file (display file contents in text area)
 */

package ClientServer;
// Class that tests the Client.
import javax.swing.JFrame;

public class ClientTest
{
   public static void main( String[] args )
   {
      Client application = new Client(); // create client application
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   } // end main
} // end class ClientTest

ServerTest class:

Code:
/*
 * @author      Jonathan Smith
 * Date:        April 8, 2011
 * Assignment:  Phase 1 DB 2
 * Course:      IT315-1102C-02
 * Instructor:  Michael Williamson
 * College:     CTU Online
 *
 * Purpose:     To read in a file from a server where the file name is dictated by the user.
 * If the file exists, display file in text area, else, display file not found error message
 *
 * Testing: Attempt to retrieve file that does not exist (error displays),
 * then retrieve existing file (display file contents in text area)
 */

package ClientServer;
// Test the Server application.
import javax.swing.JFrame;

public class ServerTest
{
   public static void main( String[] args )
   {
      Server application = new Server(); // create server
      application.runServer(); // run server application
   } // end main
} // end class ServerTest

File to be read: (Simple text file placed in the main project folder)
Music.txt
Code:
Grenade - Bruno Mars
Pink - Aerosmith
Flight of the Bumblebees - Beethoven

To run the program correctly, first run the ServerTest class. Once the server is running, run the Client test class. In the text field, enter Music.txt and press enter.

I am running Netbeans IDE 6.9 to build the project

I know the problem is in this line:

Code:
output.format( result ); // output line of file

*located in the Server class* and commented for easy find. Original code for this line was:

Code:
output.format( "%s\n", result ); // output line of file
 
Physics news on Phys.org
I think your problem is the while loop in your actionPerformed method.
Code:
while ( input.hasNextLine() ); <---- Right here[/color]
{
     inputLine = input.nextLine(); // read response from server
     displayArea.append(inputLine + "\n"); // add line to text area
} // end while
That semicolon makes for an empty while loop body, followed by the block in braces. With the semicolon, the block has nothing to do with the loop.

Get rid of that semicolon.
 
Ok, I removed the semicolon in the place you suggested, and the program loops, but I still have a formatting issue.

I would like to have the data displayed in the following manner:

The file contains:
Grenade - Bruno Mars
Pink - Aerosmith
Flight of The Bumblebees - Beethoven

However, if i place the formatter in the right place:

Code:
 while (fileInput.hasNextLine() )   // while there are more lines in the file
             {
                 result = fileInput.nextLine(); // read a line from the file
                 output.format( result ); // output line of file  **PROBLEM HERE**
                 output.flush();                // send to client
             } // end while
         } // end if

like this:

Code:
 output.format( "%s\n", result ); // output line of file  **PROBLEM HERE**

the only output that shows up is:

The file contains:Grenade-Bruno Mars

Nothing else displays after this.

If I remove the formatter, the output is as follows:

The file contains:Grenade - Bruno Mars Pink-Aerosmith Flight of the BumbleBees - Beethoven

Any suggestions on why the formatter isn't working. I know it should work with the scanner class, but I think I am still missing something.
 
iamjon.smith said:
Ok, I removed the semicolon in the place you suggested, and the program loops, but I still have a formatting issue.

I would like to have the data displayed in the following manner:

The file contains:
Grenade - Bruno Mars
Pink - Aerosmith
Flight of The Bumblebees - Beethoven

However, if i place the formatter in the right place:

Code:
  while (fileInput.hasNextLine() )   // while there are more lines in the file
  {
     result = fileInput.nextLine(); // read a line from the file
     output.format( result ); // output line of file  **PROBLEM HERE**
     output.flush();                // send to client
  } // end while
} // end if

like this:

Code:
 output.format( "%s\n", result ); // output line of file  **PROBLEM HERE**

the only output that shows up is:

The file contains:Grenade-Bruno Mars

Nothing else displays after this.

If I remove the formatter, the output is as follows:

The file contains:Grenade - Bruno Mars Pink-Aerosmith Flight of the BumbleBees - Beethoven

Any suggestions on why the formatter isn't working. I know it should work with the scanner class, but I think I am still missing something.

I don't think you should need to flush output in each loop iteration. I'm not sure, but that could be contributing to the problem. I would flush the output once, down in the finally block before closing the output file.

If that doesn't fix things, use whatever you're using for debugging, and set a breakpoint at the first statement in the while loop body. With three lines of text in your input file, the while loop should be executing three times. Setting a breakpoint and single-stepping through your code after the breakpoint is hit should shed some light on why you're not getting all of the output you're expecting.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
7K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
11K