📄 server.java
字号:
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
/**
* @param args
*/
public static List<User> userList = new ArrayList<User>();
public static void main(String args[]) {
String registryURL,portNum;
try{
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
System.out.println("Enter the RMIregistry port number:");
portNum = (br.readLine()).trim();
int RMIPortNum = Integer.parseInt(portNum);
startRegistry(RMIPortNum);
AgendaImpl exportedObj = new AgendaImpl();
System.setSecurityManager( new RMISecurityManager());
registryURL = "rmi://localhost:" + portNum+ "/Agenda";
Naming.rebind(registryURL, exportedObj);
/**/ System.out.println
/**/ ("Server registered. Registry currently contains:");
/**/ // list names currently in the registry
/**/ listRegistry(registryURL);
System.out.println("Agenda Server ready.");
}// end try
catch (Exception re) {
System.out.println("Exception in Server.main: " + re);
} // end catch
} // end main
// This method starts a RMI registry on the local host, if it
// does not already exists at the specified port number.
private static void startRegistry(int RMIPortNum)
throws RemoteException{
try {
Registry registry = LocateRegistry.getRegistry(RMIPortNum);
registry.list( ); // This call will throw an exception
// if the registry does not already exist
}
catch (RemoteException e) {
// No valid registry at that port.
/**/ System.out.println
/**/ ("RMI registry cannot be located at port "
/**/ + RMIPortNum);
Registry registry =
LocateRegistry.createRegistry(RMIPortNum);
/**/ System.out.println(
/**/ "RMI registry created at port " + RMIPortNum);
}
} // end startRegistry
// This method lists the names registered with a Registry object
private static void listRegistry(String registryURL)
throws RemoteException, MalformedURLException {
System.out.println("Registry " + registryURL + " contains: ");
String [ ] names = Naming.list(registryURL);
for (int i=0; i < names.length; i++)
System.out.println(names[i]);
} //end listRegistry
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -