1

我创建了一个必要的 RMI 类,客户端可以发送一条消息并将其打印在服务器窗口中。

在服务器上,我想阅读消息,例如,如果消息等于 100,则打印出一条消息。

问题是当我运行服务器时,我立即得到一个空指针异常。当我将消息 100 从客户端发送到服务器时,它会打印消息但不会执行 if 语句。

界面:

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

public interface FileInterface extends Remote {

    void message(String message) throws RemoteException;

}

执行:

import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class FileImpl extends UnicastRemoteObject implements FileInterface  {

private String name;

    public FileImpl(String s) throws RemoteException {
        super();
        name = s;
    }

    public void message(String message) throws RemoteException{
    System.out.println(message);
  }

      } catch(Exception e) {
         System.err.println("FileServer exception: "+ e.getMessage());
         e.printStackTrace();
         return(null);
      }
   }
} 

客户:

import java.io.*; 
import java.rmi.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

public class FileClient{

    public static boolean loggedIn = false;

   public static void main(String argv[]) {
      try {

        int RMIPort;         
        String hostName;
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(is);
        System.out.println("Enter the RMIRegistry host namer:");
        hostName = br.readLine();
        if( hostName.length() == 0 ) // if user did not enter a name
                hostName = "localhost"; // use the default host name
        System.out.println("Enter the RMIregistry port number:");
        String portNum = br.readLine();
        if( portNum.length() == 0 ) 
                portNum = "1097"; // default port number
        RMIPort = Integer.parseInt(portNum);
        String registryURL = "rmi://" + hostName+ ":" + portNum + "/FileServer";  
        // find the remote object and cast it to an interface object
        FileInterface fi = (FileInterface) Naming.lookup(registryURL);
        System.out.println("Lookup completed " );
        //System.out.println("File " + argv[0] + " Transfered" );
        // invoke the remote method
        boolean done = false;
         //file.getName()



        while( !done ) {

            System.out.println("Enter Code: 100 = Login, 200 = Upload, 300 = Download, 400 = Logout: ");
            String message = br.readLine();
            boolean messageOK = false;

            if( message.equals("100") ) {
                messageOK = true;
                System.out.println("Enter T-Number: (Use Uppercase 'T')");
                String login = br.readLine();
                if( login.charAt(0) == 'T' ) {
                    loggedIn = true;
                    fi.message("100");
                    System.out.println("Login Successful");
                    continue;
                } else {
                    System.out.println("Login Error");
                    continue;
                }
            }

        }
    } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }
}

服务器:

import java.io.*;
import java.rmi.*;

public class FileServer {

   public static void main(String argv[]) {

      if(System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
      }
      try {
         FileInterface fi = new FileImpl("FileServer");
         Naming.rebind("//localhost:1097/FileServer", fi);
         System.out.println("Server Started...");

        boolean done = false;
        while( !done ) {

        if ((msg.trim()).equals("100")) {

            System.out.println("message recieved: 100 logged in");

         } else {
            System.out.println("Login Error");
         }  

        }

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

这里的任何帮助将不胜感激。

4

0 回答 0