gpt4 book ai didi

java - RMI 客户端 java.rmi.ConnectException

转载 作者:行者123 更新时间:2023-12-01 13:16:37 27 4
gpt4 key购买 nike

我在连接客户端类的远程对象时遇到一些问题。当我运行以下客户端类时,我收到 java.rmi.ConnectException。

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

public class Client {
/**
* Declare Payment Stub object.
*/
private static Payment stub = null;

private Client() {

}

public static void main(String[] args) {
double principal = 80000;
double annualInterest = .065;
int years = 15;
/**
* Try to connect the server and look up the
* defined stub.
*/
try {
Registry reg = LocateRegistry.getRegistry("localhost");
stub = (Payment) reg.lookup("Mortgage");

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

服务器端如下:

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

public class Server {

public Server() {
}

public static void main(String args[]) {

try {
PaymentImpl robj = new PaymentImpl();
Payment stub = (Payment) UnicastRemoteObject.exportObject(robj, 9260);

Registry registry = LocateRegistry.createRegistry(9260);
registry.bind("Mortgage", stub);
System.out.println("Mortgage Server is ready to listen... ");

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

我运行 Client 类时的异常是:

    Client exception thrown: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at Client.main(Client.java:24)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 6 more

最佳答案

问题出在客户端的端口号上。获取Registry 时还要指定端口号。

LocateRegistry.getRegistry("localhost") 的 Java 文档说:

返回对默认注册表端口 1099 上的本地主机的远程对象注册表的引用。

尝试

Client.java(指定端口号)

Registry reg = LocateRegistry.getRegistry("localhost",9260);

Server.java(使用默认端口号)

Registry registry = LocateRegistry.createRegistry(1099);

关于java - RMI 客户端 java.rmi.ConnectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22418278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com