从客户端RMI中读取Server中的消息

时间:2012-12-20 15:47:58

标签: java client-server rmi

我创建了必要的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();
      }
   }
}

任何帮助都将不胜感激。

0 个答案:

没有答案