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