Java RMI Server不打印输出

时间:2017-10-14 10:52:10

标签: java server rmi

我的RMI素数程序无法在Server类中打印输出。素数的范围是从Client类输入的。

PrimeNumber

 package example.primenumbers;

 import java.rmi.Remote;
 import java.rmi.RemoteException;

 public interface PrimeNumber extends Remote{

 public String sayPrimeNumber(int n) throws RemoteException;
  }

Server.java

package example.primenumbers;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements PrimeNumber{

public Server() {}

public String sayPrimeNumber(){

  int i =0;
  int n =0;
  int num =0;
  String  primeNumbers = "";
  for (i = 1; i <= n; i++)         
  {                   
     int counter=0;           
     for(num =i; num>=1; num--)
     {
    if(i%num==0)
    {
    counter = counter + 1;
    }
 }
 if (counter ==2)
 {
    //Appended the Prime number to the String
    primeNumbers = primeNumbers + i + " ";
 }

  }

      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
  return primeNumbers;
    }



public static void main(String args[]) {

    try {
        Server obj = new Server();
        PrimeNumber stub = (PrimeNumber) UnicastRemoteObject.exportObject(obj, 0);

        // Bind the remote object's stub in the registry
        Registry registry = LocateRegistry.getRegistry();
        registry.bind("PrimeNumber", stub);

      System.err.println("Server ready");
    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
     }
   }
 }

客户端

package example.primenumbers;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client {

private Client() {}

public static void main(String[] args) 

{
  long startTime = System.nanoTime();

  Scanner scanner = new Scanner(System.in);
  int i =0;
  int num =0;
  //Empty String
  String  primeNumbers = "";
  System.out.println("Enter the value of n:");
  int n = scanner.nextInt();
  scanner.close();
  for (i = 1; i <= n; i++)         
  {                   
     int counter=0;           
     for(num =i; num>=1; num--)
     {
    if(i%num==0)
    {
    counter = counter + 1;
    }
 }
 if (counter ==2)
 {
    //Appended the Prime number to the String
    primeNumbers = primeNumbers + i + " ";
 }  
  } 

    String host = (args.length < 1) ? null : args[0];
    try {
        Registry registry = LocateRegistry.getRegistry(host);
        PrimeNumber stub = (PrimeNumber) registry.lookup("PrimeNumber");
        String response = stub.sayPrimeNumber();

    long endTime = System.nanoTime();
    long duration = (endTime - startTime)/1000000000;

    } catch (Exception e) {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
      }
   }
}

我尝试更改println并返回,但仍然没有发生任何事情。我希望有人可以帮助我。此外,这是我第一次做RMI所以请原谅我的可怕知识。

2 个答案:

答案 0 :(得分:1)

代码有两个问题

  • 它始终从0开始运行,因为服务器端的n没有更改,因此没有计算因子,因此输出的所有因子都是Prime numbers from 1 to n are :
  • 输出位于服务器端,而不是客户端程序中,因此客户端不输出任何内容

第一个的修复是发送n作为参数,第二个的修复是发回返回值并从客户端打印。

上的打印代码

Client.java

package example.primenumbers;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client {

    private Client() {}

    public static void main(final String[] args) {
        final long startTime = System.nanoTime();

        final int n;
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter the value of n:");
            n = scanner.nextInt();
        }
        final String host = (args.length < 1) ? null : args[0];
        try {
            final Registry registry = LocateRegistry.getRegistry(host);
            final PrimeNumber stub = (PrimeNumber) registry.lookup("PrimeNumber");
            final String response = stub.sayPrimeNumber(n);

            System.out.println(response);
            final long endTime = System.nanoTime();
            final long duration = (endTime - startTime)/1000000000;
            System.out.println("Took: " + duration);
        } catch (final RemoteException | NotBoundException e) {
            System.err.println("Client exception: " + e);
            e.printStackTrace();
        }
    }
}

PrimeNumber.java

public interface PrimeNumber extends Remote {
    public String sayPrimeNumber(int n) throws RemoteException;
}

Server.java

package example.primenumbers;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements PrimeNumber{

    public Server() {}

    public String sayPrimeNumber(final int n) throws RemoteException {

        String  primeNumbers = "";
        for (int i = 1; i <= n; i++)
        {
            int counter=0;
            for(int num = i; num>=1; num--)
            {
                if(i%num==0)
                {
                    counter = counter + 1;
                }
            }
            if (counter ==2)
            {
                //Appended the Prime number to the String
                primeNumbers = primeNumbers + i + " ";
            }
        }
        System.out.println("Prime numbers from 1 to n are :");
        System.out.println(primeNumbers);
        return primeNumbers;
    }

    public static void main(final String[] args) {
        try {
            final Server obj = new Server();
            final PrimeNumber stub = (PrimeNumber) UnicastRemoteObject.exportObject(obj, 0);

            // Bind the remote object's stub in the registry
            final Registry registry = LocateRegistry.getRegistry();
            registry.bind("PrimeNumber", stub);

            System.err.println("Server ready");
        } catch (final Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:-1)

服务器中的此循环永远不会运行

int i =0;
int n =0;
... 
for (i = 1; i <= n;  // here, i < 0 is never true 

您需要将n作为参数传递给方法,并将其从客户端发送到服务器,而不是将其分配给0

public String sayPrimeNumber(int n){
    int num =0;
    String  primeNumbers = "";
    for (int i = 1; i <= n; i++)        

在客户端

int n = scanner.nextInt();
stub.sayPrimeNumber(n);

您还需要从客户端删除主要计算并实际打印响应变量