//In this program, just simply demonstrate how remote methods //are called. there are four methods add, subtract, time and divide //that are resided in different server respectly. Client can invoke //all of them. import java.applet.*; import java.util.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.rmi.*; public class calclientApplet extends Applet implements ActionListener, ItemListener { addInterface ai; subtractInterface si; timeInterface ti; divideInterface di; private TextArea ta; private TextField tf; private Choice ch; private TextField tf2; private TextField tf3; Button b; Panel p; int i, i2; String ss; String font="Courier"; int fSize=16; String newLine = System.getProperty("line.separator"); public void init() { // int result; // String textIP = " "; // String serverName = "localhost"; // InetAddress server; // try { // server = InetAddress.getByName(serverName); // textIP = toText(server.getAddress()); // System.out.println("address is: "+textIP); // } catch (java.net.UnknownHostException x) { // System.out.println("unknown host!"); // x.printStackTrace ();} // try { // addServerImpl ASI = (addServerImpl) // //Naming.lookup("//localhost" + "/addServer"); // Naming.lookup("//" + textIP + "/addServer"); // result = ASI.add(3,5); // System.out.println("result: "+result); // } catch (Exception e) { // System.out.println("calclientApplet exception: " + // e.getMessage()); // e.printStackTrace(); // } Label la=new Label("Show client and all server name, IP address:"); add(la); ta=new TextArea(10,55); add(ta); Label la2=new Label("Simple calculator:"); add(la2); Panel p=new Panel(); tf=new TextField(11); ch=new Choice(); ch.addItem(" "); ch.addItem(" + "); ch.addItem(" - "); ch.addItem(" * "); ch.addItem(" / "); tf2=new TextField(11); tf3=new TextField(11); Button b = new Button("="); p.add(tf); p.add(ch); p.add(tf2); p.add(b); p.add(tf3); add(p); ta.setBackground(Color.green); ta.setFont(new Font(font, Font.BOLD, fSize)); la.setFont(new Font(font, Font.BOLD, fSize)); la2.setFont(new Font(font, Font.BOLD, fSize)); tf.addActionListener(this); tf2.addActionListener(this); tf3.addActionListener(this); b.addActionListener(this); ch.addItemListener(this); try{ ai=(addInterface)Naming.lookup("//localhost"+"/addserver"); si=(subtractInterface)Naming.lookup("//localhost"+"/subtractserver"); ti=(timeInterface)Naming.lookup("//localhost"+"/timeserver"); di=(divideInterface)Naming.lookup("//localhost"+"/divideserver"); } catch (NotBoundException e) { System.out.println("Catch NotBoundException error."); System.out.println("NotBoundException"+e.getMessage()); e.printStackTrace(); System.exit(0);} catch (RemoteException e) { System.out.println("RemoteException"+e.getMessage()); e.printStackTrace(); System.exit(0);} catch (java.net.MalformedURLException e) { System.out.println("MalformedURL error:"+e.getMessage()); e.printStackTrace(); System.exit(0);} } static String toText (byte ip[]) { /* Make portable for 128 bit format */ StringBuffer result = new StringBuffer (); for (int i = 0; i < ip.length; ++ i) { if (i > 0) result.append ("."); result.append (0xff & ip[i]); } return result.toString (); } public void itemStateChanged(ItemEvent e) { Object o=e.getItemSelectable(); if(o==ch) ss=ch.getSelectedItem();} public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="=") { int i=Integer.parseInt(tf.getText()); int i2=Integer.parseInt(tf2.getText()); if(ss==" + "){ try{ int I=ai.add(i,i2); //call remote method in the addServer tf3.setText(Integer.toString(I)); ta.setText("I am a addServer, I invoke the + operation"); } catch (RemoteException e1) { System.out.println("Add Error:" +e1); System.exit(0); } } if(ss==" - "){ try{ int I=si.subtract(i,i2); //call remote method in the subtractServer tf3.setText(Integer.toString(I)); ta.setText("I am a subtractServer, I invoke the - operation"); } catch (RemoteException e1) { System.out.println("Add Error:" +e1); System.exit(0); } } if(ss==" * "){ try{ int I=ti.time(i,i2); //call remote method in the timeServer tf3.setText(Integer.toString(I)); ta.setText("I am a timeServer, I invoke time operation"); } catch (RemoteException e1) { System.out.println("Add Error:" +e1); System.exit(0); } } if(ss==" / "){ try{ int I=di.divide(i,i2); //call remote method in the divideServer tf3.setText(Integer.toString(I)); ta.setText("I am a divideServer, I invoke the / operation"); } catch (RemoteException e1) { System.out.println("Add Error:" +e1); System.exit(0); } } }}}