I was able to make the connection and send streams back and forth...here is an example of the client side. I chose to put the code for reading the entry in this actionListener since it was already performing that role.
public Client(String host) {
super("Client Window");
chatServer = host; // set server to which this client connects
enterField = new JTextField(); // create enterField
enterField.setEditable(false);
enterField.addActionListener(
new ActionListener() {
// send message to server
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
enterField.setText("");
try // display contents of file
{
// create Socket to make connection to server
messoutput = new Formatter(client.getOutputStream());
// messoutput.flush(); // flush output to send header information
messinput = new Scanner(client.getInputStream());
String fileName = event.getActionCommand() + "\n";
messoutput.format(fileName);
messoutput.flush(); // flush output
String inputLine = messinput.nextLine(); // read input line
displayArea.setText(inputLine); // show input line in textarea
// if file exists, display file contents
if (inputLine.equals("The file is:")) {
while (messinput.hasNextLine()) {
inputLine = messinput.nextLine(); // read a new line
displayArea.append('\n' + inputLine); // add line
} // end while
} // end if
} // end try
catch (IOException ioException) {
ioException.printStackTrace();
System.exit(1);
} // end catch
finally {
try {
input.close(); // close output
output.close(); // close input
client.close(); // close connection to server
} catch (IOException ioException) {
ioException.printStackTrace();
System.exit(1);
} // end method actionPerformed
} // end anonymous inner class
}
}); // end call to addActionListener
//close the attempted file formatter scanner
add(enterField, BorderLayout.NORTH);
displayArea = new JTextArea(); // create displayArea
add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(300, 150); // set size of window
setVisible(true); // show window
} // end Client constructor
The server side...I have the get streams and process connections methods and I chose to use the process connection() for looking to see if the file actually there.
private void processConnection() throws IOException
{
String message = "Connection successful";
sendData( message ); // send connection successful message
// enable enterField so server user can send messages
setTextFieldEditable( true );
do // process messages sent from client
{
try // read message and display it
{
message = ( String ) input.readObject(); // read new message
displayMessage( "\n" + message ); // display message
//Attempted code to read format of entry and determine if it is a file
//start code
connection = server.accept(); // accept connection
messoutput = new Formatter( connection.getOutputStream() );
messoutput.flush(); // flush output to send header information
messinput = new Scanner( connection.getInputStream() );
File file = new File( messinput.nextLine() ); // get file name
String result; // result from checking file
// file does exist
if ( file.exists() )
{
Scanner fileInput = new Scanner( file ); // file scanner
messoutput.format( "The file is:\n" ); // write header
messoutput.flush(); // flush output
while ( fileInput.hasNextLine() )
{
result = fileInput.nextLine(); // read a line from file
messoutput.format( "%s\n", result ); // output line of file
messoutput.flush(); // flush output
} // end while
} // end if
else // file does not exist
{
result = file.getName() + " does not exist\n";
messoutput.format( result ); // write that file does not exist
messoutput.flush(); // flush output
} // end else
// } // end try
//end attempted code for reading format if entry
} // end try
catch ( ClassNotFoundException classNotFoundException )
{
displayMessage( "\nUnknown object type received" );
} // end catch
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
} // end method processConnection