solution

Program Echo is similar to program TCPEchoClient from Sect. 2.2.1 , but has a GUI front-end similar to that of program GetRemoteTime from Sect. 2.3 . It provides an implementation of the echo protocol (on port 7). This implementation sends one-line messages to a server and uses the following components: • a text field for input of messages (in addition to the text fi eld for input of host name); • a text area for the (cumulative) echoed responses from the server; • a button to close the connection to the host. Some of the code for this program has been provided for you in file Echo.java , a printed copy of which appears at the end of this chapter. Examine this code and make the necessary additions in the places indicated by the commented lines. When you have completed the program, run it and supply the name of any convenient server when prompted for a server name. If you don’t have access to a convenient server use localhost , having changed the port number of TCPEchoServer (Sect. 2.2.1 ) to 7 and then started that program running. //For use with exercise 2.2. import java.io.*; import java.net.*; import java.util.*; public class EmailServer { private static ServerSocket serverSocket; private static fi nal int PORT = 1234; private static fi nal String client1 = “Dave”; private static fi nal String client2 = “Karen”; private static fi nal int MAX_MESSAGES = 10; private static String[] mailbox1 = new String[MAX_MESSAGES]; private static String[] mailbox2 = new String[MAX_MESSAGES]; private static int messagesInBox1 = 0; private static int messagesInBox2 = 0; public static void main(String[] args) { System.out.println(“Opening connection…n”); try { serverSocket = new ServerSocket(PORT); } catch(IOException ioEx) { System.out.println( “Unable to attach to port!”); System.exit(1); } do { try { runService(); } catch (InvalidClientException icException) { System.out.println(“Error: ” + icException); } catch (InvalidRequestException irException) { System.out.println(“Error: ” + irException); } }while (true); } private static void runService() throws InvalidClientException, InvalidRequestException { try { Socket link = serverSocket.accept(); Scanner input = new Scanner(link.getInputStream()); PrintWriter output = new PrintWriter( link.getOutputStream(),true); String name = input.nextLine(); String sendRead = input.nextLine(); if (!name.equals(client1) && !name.equals(client2)) throw new InvalidClientException(); if (!sendRead.equals(“send”) && !sendRead.equals(“read”)) throw new InvalidRequestException(); System.out.println(“n” + name + ” ” + sendRead + “ing mail…”); if (name.equals(client1)) { if (sendRead.equals(“send”)) { doSend(mailbox2,messagesInBox2,input); if (messagesInBox2<> messagesInBox2++; } else { doRead(mailbox1,messagesInBox1,output); messagesInBox1 = 0; } } else //From client2. { if (sendRead.equals(“send”)) { doSend(mailbox1,messagesInBox1,input); if (messagesInBox1<> messagesInBox1++; } else { doRead(mailbox2,messagesInBox2,output); messagesInBox2 = 0; } } link.close(); } catch(IOException ioEx) { ioEx.printStackTrace(); } } private static void doSend(String[] mailbox, int messagesInBox, Scanner input) { /* Client has requested ‘sending’, so server must read message from this client and then place message into message box for other client (if there is room). */ String message = input.nextLine(); if (messagesInBox == MAX_MESSAGES) System.out.println(“nMessage box full!”); else mailbox[messagesInBox] = message; } private static void doRead(String[] mailbox, int messagesInBox, PrintWriter output) { /* Client has requested ‘reading’, so server must read messages from other client’s message box and then send those messages to the fi rst client. */ System.out.println(“nSending ” + messagesInBox + ” message(s).n”); output.println(messagesInBox); for (int i=0; i output.println(mailbox[i]); } } class InvalidClientException extends Exception { public InvalidClientException() { super(“Invalid client name!”); } public InvalidClientException(String message) { super(message); } } class InvalidRequestException extends Exception { public InvalidRequestException() { super(“Invalid request!”); } public InvalidRequestException(String message) { super(message); } } //For use with exercise 2.2. import java.io.*; import java.net.*; import java.util.*; public class EmailClient { private static InetAddress host; private static fi nal int PORT = 1234; private static String name; private static Scanner networkInput, userEntry; private static PrintWriter networkOutput; public static void main(String[] args) throws IOException { try { host = InetAddress.getLocalHost(); } catch(UnknownHostException uhEx) { System.out.println(“Host ID not found!”); System.exit(1); } userEntry = new Scanner(System.in); do { System.out.print( “nEnter name (‘Dave’ or ‘Karen’): “); name = userEntry.nextLine(); }while (!name.equals(“Dave”) && !name.equals(“Karen”)); talkToServer(); } private static void talkToServer() throws IOException { String option, message, response; do { /****************************************************** CREATE A SOCKET, SET UP INPUT AND OUTPUT STREAMS, ACCEPT THE USER’S REQUEST, CALL UP THE APPROPRIATE METHOD (doSend OR doRead), CLOSE THE LINK AND THEN ASK IF USER WANTS TO DO ANOTHER READ/SEND. ******************************************************/ }while (!option.equals(“n”)); } private static void doSend() { System.out.println(“nEnter 1-line message: “); String message = userEntry.nextLine(); networkOutput.println(name); networkOutput.println(“send”); networkOutput.println(message); } private static void doRead() throws IOException { /********************************* BODY OF THIS METHOD REQUIRED *********************************/ } } ______________________________________________________ //For use with exercise 2.4. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; import java.io.*; import java.util.*; public class Echo extends JFrame implements ActionListener { private JTextField hostInput,lineToSend; private JLabel hostPrompt,messagePrompt; private JTextArea received; private JButton closeConnection; private JPanel hostPanel,entryPanel; private fi nal int ECHO = 7; private static Socket socket = null; private Scanner input; private PrintWriter output; public static void main(String[] args) { Echo frame = new Echo(); frame.setSize(600,400); frame.setVisible(true); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { if (socket != null) { try { socket.close(); } catch (IOException ioEx) { System.out.println( “n* Unable to close link! *n”); System.exit(1); } System.exit(0); } } } ); } public Echo() { hostPanel = new JPanel(); hostPrompt = new JLabel(“Enter host name:”); hostInput = new JTextField(20); hostInput.addActionListener(this); hostPanel.add(hostPrompt); hostPanel.add(hostInput); add(hostPanel, BorderLayout.NORTH); entryPanel = new JPanel(); messagePrompt = new JLabel(“Enter text:”); lineToSend = new JTextField(15); //Change fi eld to editable when // host name entered… lineToSend.setEditable(false); lineToSend.addActionListener(this); /************************************************ * ADD COMPONENTS TO PANEL AND APPLICATION FRAME * ************************************************/ /******************************************** * NOW SET UP TEXT AREA AND THE CLOSE BUTTON * ********************************************/ } public void actionPerformed(ActionEvent event) { if (event.getSource() == closeConnection) { if (socket != null) { try { socket.close(); } catch(IOException ioEx) { System.out.println( “n* Unable to close link!*n”); System.exit(1); } lineToSend.setEditable(false); hostInput.grabFocus(); } return; } if (event.getSource() == lineToSend) { /******************/ * SUPPLY CODE HERE * *******************/ } //Must have been entry into host fi eld… String host = hostInput.getText(); try { /******************* * SUPPLY CODE HERE * *******************/ } catch (UnknownHostException uhEx) { received.append(“n*** No such host! ***n”); hostInput.setText(“”); } catch (IOException ioEx) { received.append(“n*** ” + ioEx.toString() + ” ***n”); } } }
 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!