- #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
Server Class:
Client Test Class:
ServerTest class:
File to be read: (Simple text file placed in the main project folder)
Music.txt
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:
*located in the Server class* and commented for easy find. Original code for this line was:
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