Advanced Java: Client/Server Application Problem

In summary, the conversation discusses the code for a client and server Java Swing application that allows the client to specify a file name to the server and receive the contents of the file back if it exists. The program runs as expected except for a formatting issue with the output. The programmer is using a formatter from the Scanner class, but it stops writing after the first line. They are looking for ideas on how to fix this issue. The code for the client and server classes is also provided.
  • #1
iamjon.smith
117
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
  • #2
I think your problem is the while loop in your actionPerformed method.
Code:
while ( input.hasNextLine() ); [color="red"]<---- 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.
 
  • #3
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.
 
  • #4
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.
 
  • #5


I am trying to format the output so that each line of the file is on its own line in the text area, but when I do this, it only outputs the first line of the file. If I remove the formatting, it outputs the whole file, but not on separate lines.



There are a few things that could be causing the formatting issue in your code. Here are a few suggestions to try and fix it:

1. Make sure you are using the correct escape character for new lines. In your code, you are using "\n" which is the correct escape character for new lines, so this should not be causing the issue.

2. Check the documentation for the Formatter class to make sure you are using the correct format string. According to the documentation, the format string "%s\n" should work for printing each line on a new line. However, you could also try using "%s%n" which is the platform-specific line separator.

3. Instead of using the Formatter class, try using the PrintWriter class. This class has a method called "println" which automatically appends a new line to the end of each string. So you could replace your problematic line with:

output.println(result);

4. Make sure you are closing your output stream after writing to it. In your code, you are closing it in the finally block, but it would be better to close it right after you are done writing to it. This could potentially be causing issues with the formatting.

5. Check the contents of your Music.txt file. It is possible that there are some hidden characters or formatting in the file that is causing the issue. Try creating a new text file with just a few lines of plain text and see if the formatting works correctly with that file.

Hopefully one of these suggestions will help you fix the formatting issue in your code. Good luck!
 

Related to Advanced Java: Client/Server Application Problem

1. What is Advanced Java?

Advanced Java is a programming language that builds upon the basic Java language by providing additional features and functionality. It allows for more complex and sophisticated applications to be developed, including client/server applications.

2. What is a Client/Server Application?

A client/server application is a type of software architecture where a central server provides services to multiple client devices, such as computers or mobile devices. The client sends requests to the server, which then processes and responds to them.

3. What are the benefits of using Advanced Java for Client/Server Applications?

Advanced Java offers several benefits for developing client/server applications, including platform independence, multi-threading capabilities, and built-in networking libraries. It also provides a robust framework for handling complex data and user interactions.

4. What are some common problems that can arise when developing Client/Server Applications in Advanced Java?

Some common problems that can arise when developing client/server applications in Advanced Java include network connectivity issues, security vulnerabilities, and scalability challenges. It is important to carefully plan and design the application to address these potential problems.

5. How can I improve the performance of my Advanced Java Client/Server Application?

To improve the performance of an Advanced Java client/server application, you can use techniques such as caching, optimizing database queries, and implementing load balancing. It is also important to regularly monitor and analyze the application's performance to identify and address any bottlenecks or inefficiencies.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
31
Views
11K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top