Monday, January 7, 2008

Java Example Event Programming

Important points


import java.rmi.* ; all methods in this package throws
RemoteException that must be caught.
Remote in interface indicates that this is remote interface,
the methods can be used by object in the remote machine.
Server extends UnicastRemoteObject implements
remote interface
Also import java.rmi.server.* for server program
Naming.rebind("servername", serverobject);
Others methods are bind , rebind, unbind
In client, Naming.lookup("rmi://ipaddress/servername") method is used to identify the server and returns remote interface. 6. Use inter.method to call the method in client.
Steps to exucute RMI
Write programs for interface, server and client and compile them.
javac inter.java
javac server.java
javac client.java or javac *.java
2. Then use rmi compiler to create stub and skeleton class.
rmic server
3. Copy inter, server, skeleton to server machine and
inter, client and stub to client program
4. Then start rmi registry by
start rmiregistry
5. Run the server program in server machine by
java server
6. Execute the client program by
java client ipaddress arg1 arg2
java client 200.200.1.120 5 8

import java.rmi.*;
public class client
{
public static void main(String arg[])
{
try
{
int a = Integer.parseInt(arg[1]);
int b = Integer.parseInt(arg[2]);
int result;
inter i = (inter) Naming.lookup("rmi://" + arg[0] + "/Addserver");
System.out.println("client");
i.getdata(a,b);
result = i.adddata();
System.out.println(result);
}
catch(Exception e)
{
System.out.println("error " + e);
}
}
}
/* Example for deserialization
ObjectInputStream(FileInputStream f)
*/
import java.io.*;
class myclass implements Serializable
{
String s;
int i;
int j;
myclass(String s1,int m,int n)
{
s=s1; i=m;j=n;
}
public String toString()
{
return (s + i + j);
}
}
class deser
{
public static void main(String arg[]) throws Exception
{
myclass ob2 ;
FileInputStream f = new FileInputStream("obj");
ObjectInputStream o = new ObjectInputStream(f);
ob2 = (myclass) o.readObject();
System.out.println(ob2.s + ob2.i);
o.close();
System.out.println("Obeject2 " + ob2);
}
}
import java.rmi.*;
public interface inter extends Remote
{
public void getdata(int m,int n) throws RemoteException;
int adddata() throws RemoteException;
}
/* Example for serialization
ObjectOutputStream(FileOutputStream f)
o.writeObject(ob)
*/
import java.io.*;
class myclass implements Serializable
{
String s;
int i;
int j;
myclass(String s1,int m,int n)
{
s=s1; i=m;j=n;
}
}
class ser
{
public static void main(String arg[]) throws IOException, NotSerializableException
{
myclass ob = new myclass("Murali",4,5);
FileOutputStream f = new FileOutputStream("obj");
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(ob);
o.flush();
o.close();
System.out.println(ob);
}
}
import java.rmi.*;
import java.rmi.server.*;
public class server extends UnicastRemoteObject implements inter
{
int x,y;
public server() throws RemoteException
{
}
public int adddata() throws RemoteException
{
return x+y;
}
public void getdata(int m, int n) throws RemoteException
{
x=m; y=n;
}
public static void main(String arg[])
{
try
{
server s = new server();
Naming.rebind("Addserver",s);
}
catch(Exception e)
{
System.out.println("Exception e");
}
}
}
import java.rmi.*;
import java.rmiregistry.*;
public class clientport
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == submit)
{
try
{
rmiserver server = (rmiserver) Naming.lookup("rmi//localhost/connect");
String name = getData();
}
catch(Exception e)
{
System.out.pritnln("Unable to connect");
}
}
}
}
import java.sql.*;
public class details
{
private Connection connect = null;
private Statement query =null;
private ResultSet result = null;
String dsn;
public details(String dsn)
{
this.dsn = "jdbc:odbc:" + dsn ;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connect = DriverManager.getConnection(dsn,"","");
query = connect.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Connection failed");
}
}
public void setData(String Table, String data)
{
try
{
// String sql = "Insert into " + table + (name) values("'+data+'")";
String sql = "Insert into table(name) values('rrr')";
query.executeUpdate(sql);
}
catch(Exception e)
{
System.out.println("Query failed");
}
}
}
import java.rmi.*;
import java.rmi.server.*;
public class rmidatabase extends UnicastRemoteObject implements rmiserver
{
private details det=null;
public rmidatabase(String name) throws RemoteException
{
super();
try
{
Naming.rebind(name,this);
}
catch(Exception e)
{
System.out.println("Unable to bind");
}
det = new details("student");
}
public String receiveData(String s)
{
return null;
}
public void sendData(String data)
{
System.out.println("Server sending data");
det.sendData("registration",data);
}
public static void main(String a[])
{
try
{
System.setSecurityManager(new RMISecurityManager());
rmidatabase connect = new rmidatabase("connect");
System.out.println("Server started");
}
catch(Exception e)
{
System.out.println("Error in server");
}
}
}
import java.rmi.*;
interface rmiserver extends Remote
{
public void sendData(String data) throws RemoteException ;
public String receiveData(String data) throws RemoteException ;
}

No comments: