import java.awt.*; import java.net.*; public class AddressFrameSolution extends Frame { public AddressFrameSolution() { SymWindow aSymWindow = new SymWindow(); this.addWindowListener(aSymWindow); SymAction lSymAction = new SymAction(); goButton.addActionListener(lSymAction); setLayout(null); setSize(405,305); setVisible(false); goButton.setLabel("Go"); add(goButton); goButton.setBackground(java.awt.Color.lightGray); goButton.setBounds(48,48,69,30); add(ipAddress); ipAddress.setBounds(48,132,208,25); add(symbolicAddress); symbolicAddress.setBounds(48,192,209,28); ipLabel.setText("ipAddress"); add(ipLabel); ipLabel.setBounds(48,96,100,40); symbolicLabel.setText("Symbolic address"); add(symbolicLabel); symbolicLabel.setBounds(48,156,100,40); setTitle("Ex21"); } public AddressFrameSolution(String title) { this(); setTitle(title); } static public void main(String args[]) { try { ( new AddressFrameSolution()).setVisible(true); } catch (Throwable t) { System.err.println(t); t.printStackTrace(); //Ensure the application exits with an error condition. System.exit(1); } } public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension d = getSize(); super.addNotify(); if (fComponentsAdjusted) return; // Adjust components according to the insets setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height); Component components[] = getComponents(); for (int i = 0; i < components.length; i++) { Point p = components[i].getLocation(); p.translate(getInsets().left, getInsets().top); components[i].setLocation(p); } fComponentsAdjusted = true; } // Used for addNotify check. boolean fComponentsAdjusted = false; java.awt.Button goButton = new java.awt.Button(); java.awt.TextField ipAddress = new java.awt.TextField(); java.awt.TextField symbolicAddress = new java.awt.TextField(); java.awt.Label ipLabel = new java.awt.Label(); java.awt.Label symbolicLabel = new java.awt.Label(); class SymWindow extends java.awt.event.WindowAdapter { public void windowClosing(java.awt.event.WindowEvent event) { Object object = event.getSource(); if (object == AddressFrameSolution.this) Frame1_WindowClosing(event); } } void Frame1_WindowClosing(java.awt.event.WindowEvent event) { Frame1_WindowClosing_Interaction1(event); } void Frame1_WindowClosing_Interaction1(java.awt.event.WindowEvent event) { try { System.exit(0); } catch (Exception e) { } } class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == goButton) goButton_ActionPerformed(event); } } //**************************************************************************** //End of ignorable code //**************************************************************************** void goButton_ActionPerformed(java.awt.event.ActionEvent event) { try { String dottedQuadString = ipAddress.getText(); InetAddress address = InetAddress.getByName(dottedQuadString); symbolicAddress.setText(address.getHostName()); } catch (Exception exc) {} } } ------- // ECHO FRAME import java.awt.*; import java.net.*; import java.io.*; public class EchoFrameSolution extends Frame { private Socket sock; private BufferedReader bf; private PrintWriter pw; //***************************************************************************** //Ignore code until next asterisks, all it dos is to set up a set of //visual objects for the exercise //***************************************************************************** public EchoFrameSolution() { SymWindow aSymWindow = new SymWindow(); this.addWindowListener(aSymWindow); SymAction lSymAction = new SymAction(); goButton.addActionListener(lSymAction); setLayout(null); setSize(405,305); setVisible(false); goButton.setLabel("Go"); add(goButton); goButton.setBackground(java.awt.Color.lightGray); goButton.setBounds(36,36,72,24); add(messageField); messageField.setBounds(36,108,241,24); add(replyField); replyField.setBounds(36,168,241,24); add(compField); compField.setBounds(36,228,241,24); messagelabel.setText("Message"); add(messagelabel); messagelabel.setBounds(36,72,100,40); replyLabel.setText("Reply"); add(replyLabel); replyLabel.setBounds(36,144,48,24); nameLabel.setText("Computer name"); add(nameLabel); nameLabel.setBounds(36,192,100,40); setTitle("Exercise 2.2"); } public EchoFrameSolution(String title) { this(); setTitle(title); } static public void main(String args[]) { try { //Create a new instance of our application's frame, and make it visible. (new EchoFrameSolution()).setVisible(true); } catch (Throwable t) { System.err.println(t); t.printStackTrace(); //Ensure the application exits with an error condition. System.exit(1); } } public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension d = getSize(); super.addNotify(); if (fComponentsAdjusted) return; // Adjust components according to the insets setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height); Component components[] = getComponents(); for (int i = 0; i < components.length; i++) { Point p = components[i].getLocation(); p.translate(getInsets().left, getInsets().top); components[i].setLocation(p); } fComponentsAdjusted = true; } // Used for addNotify check. boolean fComponentsAdjusted = false; java.awt.Button goButton = new java.awt.Button(); java.awt.TextField messageField = new java.awt.TextField(); java.awt.TextField replyField = new java.awt.TextField(); java.awt.TextField compField = new java.awt.TextField(); java.awt.Label messagelabel = new java.awt.Label(); java.awt.Label replyLabel = new java.awt.Label(); java.awt.Label nameLabel = new java.awt.Label(); class SymWindow extends java.awt.event.WindowAdapter { public void windowClosing(java.awt.event.WindowEvent event) { Object object = event.getSource(); if (object == EchoFrameSolution.this) Frame1_WindowClosing(event); } } void Frame1_WindowClosing(java.awt.event.WindowEvent event) { Frame1_WindowClosing_Interaction1(event); } void Frame1_WindowClosing_Interaction1(java.awt.event.WindowEvent event) { try { System.exit(0); } catch (Exception e) { } } class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == goButton) goButton_ActionPerformed(event); } } //***************************************************************************** //End of ignored section //***************************************************************************** void goButton_ActionPerformed(java.awt.event.ActionEvent event) { try { //Set up socket and streams on designated computer sock = new Socket(compField.getText(),7); bf= new BufferedReader(new InputStreamReader(sock.getInputStream())); pw = new PrintWriter(sock.getOutputStream(),true); //Send message to computer String message = messageField.getText(); System.out.println(""+message); pw.println(message); //Get message back from computer String reply = bf.readLine(); //Display it replyField.setText(reply); } catch(Exception ex){ System.out.println("Problem with connection"+ex);} } } --------- //Name Client import java.awt.*; import java.io.*; import java.net.*; public class NameClientSolution extends Frame { //Instance variables for net connection static Socket ss = null; static InputStream is =null; static BufferedReader bf = null; static OutputStream os = null; static PrintWriter pw = null; //***************************************************************************** //Ignore the code up to the next line of asterisks //it just sets up the visual objects for the exercise //***************************************************************************** public NameClientSolution() { SymWindow aSymWindow = new SymWindow(); this.addWindowListener(aSymWindow); SymAction lSymAction = new SymAction(); gobutton.addActionListener(lSymAction); setLayout(null); setSize(405,305); setVisible(false); gobutton.setLabel("Go"); add(gobutton); gobutton.setBackground(java.awt.Color.lightGray); gobutton.setBounds(36,60,60,24); add(nameField); nameField.setBounds(36,144,243,27); add(addressField); addressField.setBounds(36,216,243,27); nameLabel.setText("Name"); add(nameLabel); nameLabel.setBounds(36,120,60,24); addressLabel.setText("Address"); add(addressLabel); addressLabel.setBounds(36,180,60,24); setTitle("Exercise 2.3 Client"); } public NameClientSolution(String title) { this(); setTitle(title); } static public void main(String args[]) { try { ss = new Socket(InetAddress.getLocalHost(),1024); // Set up the streams which the clients will access is = ss.getInputStream(); bf = new BufferedReader(new InputStreamReader(is)); os = ss.getOutputStream(); pw = new PrintWriter(os,true); } catch(Exception e) {System.out.println("Trouble with initial connection"+e);} try { //Create a new instance of our application's frame, and make it visible. (new NameClientSolution()).setVisible(true); } catch (Throwable t) { System.err.println(t); t.printStackTrace(); //Ensure the application exits with an error condition. System.exit(1); } } public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension d = getSize(); super.addNotify(); if (fComponentsAdjusted) return; // Adjust components according to the insets setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height); Component components[] = getComponents(); for (int i = 0; i < components.length; i++) { Point p = components[i].getLocation(); p.translate(getInsets().left, getInsets().top); components[i].setLocation(p); } fComponentsAdjusted = true; } boolean fComponentsAdjusted = false; java.awt.Button gobutton = new java.awt.Button(); java.awt.TextField nameField = new java.awt.TextField(); java.awt.TextField addressField = new java.awt.TextField(); java.awt.Label nameLabel = new java.awt.Label(); java.awt.Label addressLabel = new java.awt.Label(); class SymWindow extends java.awt.event.WindowAdapter { public void windowClosing(java.awt.event.WindowEvent event) { Object object = event.getSource(); if (object == NameClientSolution.this) Frame1_WindowClosing(event); } } void Frame1_WindowClosing(java.awt.event.WindowEvent event) { Frame1_WindowClosing_Interaction1(event); } void Frame1_WindowClosing_Interaction1(java.awt.event.WindowEvent event) { try { // QuitDialog Create and show as modal System.exit(0); } catch (Exception e) { } } class SymAction implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent event) { Object object = event.getSource(); if (object == gobutton) gobutton_ActionPerformed(event); } } //**************************************************************************** //End of ignored section //**************************************************************************** void gobutton_ActionPerformed(java.awt.event.ActionEvent event) { try { // Extract the string from the name text field and send to server pw.println(nameField.getText()); // Get reply from server String reply = bf.readLine(); // Display reply addressField.setText(reply); } catch (Exception ex) {System.out.println("Problem communicating with name server"+ex);} } }