• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with RMI code?

 
Yatin Shah
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sir,

I want to get Server System Information on vlient machine. I am not getting my result. I am sending my code here with. Please check i tand tell me What's the problem with this code?

(1) Interface:

//CountRMI.java

public interface CountRMI extends java.rmi.Remote {
public int getSum() throws java.rmi.RemoteException;
public void setSum(int val) throws java.rmi.RemoteException;
public int increment() throws java.rmi.RemoteException;
public String getProperties() throws java.rmi.RemoteException;
}

(2) Implementation:

// CountRMIImpl.java

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.awt.*;
import java.lang.*;

public class CountRMIImpl extends UnicastRemoteObject implements CountRMI
{
private int sum;
private String finalString,name,ver,user,lang,reg,cpu,jname,vmname,VmVen,VmVer,venurl;

public CountRMIImpl(String name) throws RemoteException
{

super();

try
{
Naming.rebind(name, this);
sum = 0;
finalString = ("\n\n\nSystem :\n "+name+"\n Version "+ver+"\n\n"+"Registered To :\n User "+user+"\n Language "+lang+"\n Region "+reg+"\n\n"+"Computer :\n "+cpu+"\n\n"+"Java Information :\n "+jname +"\n "+vmname+"\n "+VmVen+"\n Version "+VmVer+"\n "+venurl);
}

catch (Exception e)
{
System.out.println("Exception "+e.getMessage());
e.printStackTrace();
}
}

public int getSum() throws RemoteException
{
return sum;
}

public void setSum(int val) throws RemoteException
{
sum = val;
}

public int increment() throws RemoteException
{
return sum++;
}

public String getProperties() throws RemoteException
{
return finalString;
}

}

(3) Client Class:

//CountRMIClient.java

import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.lang.*;
import java.net.*;

public class CountRMIClient
{
public static void main(String [] args)
{
String[] s = new String[25];
String[] str = new String[25];
FileReader fr;
FileWriter fw;
PrintWriter pw;
BufferedReader br;
int lineNo=0,noLines;

try
{
fr = new FileReader("IPList.txt");
br = new BufferedReader(fr);

while((s[lineNo]=br.readLine()) != null)
lineNo++;

br.close();
fr.close();
}

catch(FileNotFoundException fnfe)
{
System.out.println("File not found.... try another");
}

catch(IOException ioe)
{
System.out.println("I/0 Exception while reading file");
}

while(true)
{
noLines = 0;

for( int j = 0; j < lineNo; j++)
{
try
{
CountRMI myCount = (CountRMI)Naming.lookup("rmi://"+s[j]+"/mycount");

str[noLines] = s[j];
noLines++;

// Set Sum to intial value of 0.

System.out.println("Setting Sum to 0");
myCount.setSum(0);

// Calculate Start time.

long startTime = System.currentTimeMillis();

// Increment 1000 times

System.out.println("Incrementing");

for(int i = 0; i < 1000; i++)
{
myCount.increment();
}

// Calaculate stop time; print out statistics.

long stopTime = System.currentTimeMillis();

System.out.println("Average Ping = "+ ((stopTime - startTime)/1000f) +" micro-second");
System.out.println("Sum = "+myCount.getSum());
System.out.println("Server "+s[j]+" is working");
System.out.println(myCount.getProperties());
System.out.println();
}

catch(RemoteException re)
{
System.err.println("Server "+s[j]+" is not ready.");
System.out.println();

}

catch(NotBoundException nb)
{
System.err.println(" File Not Found "+nb);
}

catch(MalformedURLException mfue)
{
System.err.println(" Proble with URl Change and try again....");
}
}

try
{
// Creating File for Working Server.

fw = new FileWriter("OnList.txt");
pw = new PrintWriter(fw);

for(int k = 0; k < noLines; k++)
pw.println(str[k]);

fw.close();
pw.close();
}

catch(IOException ie)
{
System.err.println(" File Not Found "+ie);
}

try
{
System.out.println("Wait Few Seconds..........");
System.out.println();
Thread.sleep(18000);
}

catch(InterruptedException ie)
{
System.out.println("Interrupted the sleep...");
}
}
}
}

(4) Server Class:

// CountRMIServer.java

import java.rmi.*;
import java.rmi.server.*;
import java.awt.*;
import java.lang.*;

public class CountRMIServer
{
public static void main(String [] args)
{
//Create and Install the security manager

try
{
CountRMIImpl myCount = new CountRMIImpl("mycount");
System.out.println("CountRMI Server ready.");

String name = System.getProperty("os.name");
String ver = System.getProperty("os.version");
String user = System.getProperty("user.name");
String lang = System.getProperty("user.language");
String reg = System.getProperty("user.region");
String cpu = System.getProperty("sun.cpu.isalist");
String jname = System.getProperty("java.runtime.name");
String vmname = System.getProperty("java.vm.name");
String VmVen = System.getProperty("java.vm.vendor");
String VmVer = System.getProperty("java.vm.version");
String jclass = System.getProperty("java.class.version");
String venurl = System.getProperty("java.vendor.url");

}

catch(Exception e)
{
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}

Please, check my code and replay me.

Thanking You
Ytain
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say you aren't "getting your result"... it would be better to start out by saying what you are expecting from your program and what is happening instead. There may not be an error in the source code (there may be, I haven't looked too closely at it yet), but the problem could come from a variety of sources: configuration issues, network problems, etc. and it would help to know exactly what problem you are having before reading through all the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!