Aim: Write an RMI application where client supplies a string and server responds by sending the string in upper case. (Topics covered: Distributed application using java.rmi package )
Client.java
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Client extends JFrame {
TextField t1 = new TextField(30);
TextField t2 = new TextField(30);
JButton b = new JButton("Uppercase");
Panel p = new Panel(new GridLayout(4, 1, 5, 5));
RemoteInterface2 s;
public Client() {
super("Client Side");
setSize(250, 250);
setLocation(300, 300);
getContentPane().add(p, "North");
p.add(t1);
p.add(t2);
p.add(b);
try {
String ipp = JOptionPane.showInputDialog("Please enter the IP Address to Connect");
String ip = "rmi://" + ipp + "/RMIAPPLICATION";
s = (RemoteInterface2) Naming.lookup(ip);
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, exp.getMessage());
}
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String a = t1.getText();
try {
String r = s.upper(a);
t2.setText("String in =" + r);
} catch (Exception epx) {
}
}
});
}
public static void main(String args[]) {
Client c = new Client();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
}
}
Server.java
import java.rmi.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
try {
ServerImplements s = new ServerImplements();
Naming.rebind("RMIAPPLICATION", s);
System.out.println("Server has been started");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Remoteinterface
import java.rmi.*;
public interface RemoteInterface2 extends Remote
{
public String upper(String x) throws Exception;
}
ServerImplements
import java.rmi.*;
import java.rmi.server.*;
public class ServerImplements extends UnicastRemoteObject implements
RemoteInterface {
public ServerImplements() throws Exception {
super();
}
public String upper(String x) throws Exception {
String y = x.toUpperCase();
return y;
}
}
Client.java
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Client extends JFrame {
TextField t1 = new TextField(30);
TextField t2 = new TextField(30);
JButton b = new JButton("Uppercase");
Panel p = new Panel(new GridLayout(4, 1, 5, 5));
RemoteInterface2 s;
public Client() {
super("Client Side");
setSize(250, 250);
setLocation(300, 300);
getContentPane().add(p, "North");
p.add(t1);
p.add(t2);
p.add(b);
try {
String ipp = JOptionPane.showInputDialog("Please enter the IP Address to Connect");
String ip = "rmi://" + ipp + "/RMIAPPLICATION";
s = (RemoteInterface2) Naming.lookup(ip);
} catch (Exception exp) {
JOptionPane.showMessageDialog(null, exp.getMessage());
}
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String a = t1.getText();
try {
String r = s.upper(a);
t2.setText("String in =" + r);
} catch (Exception epx) {
}
}
});
}
public static void main(String args[]) {
Client c = new Client();
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
}
}
Server.java
import java.rmi.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
try {
ServerImplements s = new ServerImplements();
Naming.rebind("RMIAPPLICATION", s);
System.out.println("Server has been started");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Remoteinterface
import java.rmi.*;
public interface RemoteInterface2 extends Remote
{
public String upper(String x) throws Exception;
}
ServerImplements
import java.rmi.*;
import java.rmi.server.*;
public class ServerImplements extends UnicastRemoteObject implements
RemoteInterface {
public ServerImplements() throws Exception {
super();
}
public String upper(String x) throws Exception {
String y = x.toUpperCase();
return y;
}
}
No comments:
Post a Comment