public class PrintServer {
public static void main(String args[]) {
try {
System.setProperty("java.rmi.server.hostname","192.168.1.23");
Print ps = new PrintImpl();
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://192.168.1.23/adder", ps);
} catch (AccessException e) {
System.err.println("Bind operation not permitted");
} catch (RemoteException e) {
System.err.println("Registry could not be contacted" + e);
} catch (MalformedURLException e) {
System.err.println("Wrong URL for binding");
} catch (AlreadyBoundException e) {
System.err.println("Object alreay bound to the registry");
}
}
}
public class PrintClient {
public static void main(String[] args) {
Scanner s = null;
try {
Print a = (Print)Naming.lookup("rmi://192.168.1.23/adder");
System.out.println("Insert string");
s = new Scanner(System.in);
String op1 = s.nextLine();
a.print(op1);
} catch (NotBoundException e) {
System.err.println("Request obect not bound "+ e);
} catch (MalformedURLException e) {
System.err.println("Wrong URL" + e);
} catch (RemoteException e) {
System.err.println("Network or Server Error" + e);
} finally {
if (s != null) s.close();
}
}
}
客户端进程和服务器进程在同一网络中的两台不同机器上启动。 考虑到所做的测试,是否可以始终为服务器分配相同的端口? 是否可以始终为服务器分配相同的端口?我做了一些测试并启用了端口,但它每次都会改变。我能决定吗?
客户执行:
插入字符串
你好
网络或服务器错误java.rmi.ConnectException: Connection refused > to host: 192.168.1.23;嵌套异常是:java.net.ConnectException:连接超时:connect
试用:
通过 wireshark 我看到,一旦联系了注册表,客户端就会使用 ip 192.168.1.23 与服务器通信,这是一个端口,当我重新启动服务器时会发生变化,如果我在此端口上禁用防火墙,我不会收到任何错误。