Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javarmi建立连接_Java_Rmi - Fatal编程技术网

使用javarmi建立连接

使用javarmi建立连接,java,rmi,Java,Rmi,我正在编写一个程序,使用RMI将客户机连接到服务器,到目前为止,我一直得到java.net.ConnectException:Connection-denied 这是我的密码 接口 public interface ServerInterface extends Remote { public String getMessage() throws RemoteException; } 服务器 public class Server extends UnicastRemoteObject im

我正在编写一个程序,使用RMI将客户机连接到服务器,到目前为止,我一直得到
java.net.ConnectException:Connection-denied

这是我的密码

接口

public interface ServerInterface extends Remote
{
public String getMessage() throws RemoteException;

}
服务器

public class Server extends UnicastRemoteObject implements ServerInterface 
{
int portNumber = 7776;
String ipAddress;
Registry registry;

public Server() throws RemoteException
{
    try
    {
        ipAddress = "192.168.0.104";
        System.out.println("IP Address: " + ipAddress + " Port Number: " + portNumber);
        registry = LocateRegistry.getRegistry();
        registry.rebind("ServerFour", this);
    }
    catch (RemoteException e)
    {
        System.out.println("Remote Exception Error");
    }
}

public String getMessage() throws RemoteException
{
    String output = "Connected to Server";

    return output;
}

public static void main(String args[])
{
    try
    {
        Server server = new Server();

    }
    catch (RemoteException ex)
    {
        System.out.println("Remote Exception in Main");
    }

}

}
客户

public class Client 
{
public static void main(String[] args) throws NumberFormatException, RemoteException, NotBoundException 
{
    try
    {
        ServerInterface server;
        Registry registry;

        String serverAddress = "192.168.0.104";
        String serverPort = "7776";

        registry = LocateRegistry.getRegistry(serverAddress, Integer.valueOf(serverPort));

        server = (ServerInterface)(registry.lookup("ServerFour"));

        String serverMessage = server.getMessage();

    System.out.println("Servers Message: " + serverMessage);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


}


}
现在,我只想让客户端调用ServerInterface中的方法并打印出它的消息,但我似乎无法让它工作。当我启动客户端时,我会收到上面显示的异常消息

启动服务器时,它会返回以下内容:

IP地址:client4/127.0.1.1端口号:1234

更新:

我已将端口号改为7776 注册号7776&

这是我在启动服务器并运行netstat-anpt时得到的结果

现在在客户端,我得到以下信息:

似乎RMI
注册表
绑定到
本地主机
(参见127.0.0.1-我假设127.0.1.1是一个打字错误?),但尝试在
192.168.0.104
上从客户端联系它-这不会起作用,因为该接口上可能没有任何内容在侦听!尝试将客户端
serverAddress
改为
127.0.0.1

如果您想查看哪些进程绑定到哪些接口上(顺便说一句,
t
用于TCP),您可以使用命令
netstat-anpt
(或在Windows上:
netstat-anbt

这是将注册表绑定到特定IP(例如,
localhost
)的方法


干杯,

服务器上的防火墙阻止端口1234?你能远程登录这项服务吗?请澄清“它做同样的事情”。如果使用telnet,连接被拒绝?然后,要么服务器未启动并运行,要么访问被禁止。是的,当我尝试telnet时,它返回连接被拒绝。因此,无法从该客户端访问该服务器。与RMI无关。您可以远程登录到服务器上的另一个端口吗?e、 g.23?此外,当您遇到异常时,切勿替换您自己的消息。始终打印例外情况下附带的内容。否则你就没有希望知道出了什么问题。他没有“将服务器绑定到本地主机”。他正在将远程对象绑定到本地主机中运行的RMI注册表,这是您可以绑定到的唯一注册表。这与ServerSocket的绑定地址无关,默认情况下,绑定地址为0.0.0.0,除非您指定一个RMIServerSocketFactory来执行其他操作,而他没有这样做。他的代码是正确的。我的表述非常笼统——对此表示歉意——我对我的答案做了一些修改。您所说的是正确的,但是注册表似乎绑定到本地主机(“
IP地址:client4/127.0.1.1端口号:1234
”),因此我认为我的建议仍然有效。否。注册表是用端口号创建的,没有主机地址,因此它会像上面一样绑定到0.0.0.0。程序只是打印出localhost的值:这不是任何东西绑定到的证据。将客户端“serverAddress”更改为127.0.0.1肯定没有帮助,除非客户端和服务器都在同一主机上运行,在这种情况下,一开始就不会有问题……这就是为什么我建议
netstat
看看发生了什么。我同意注册表应该绑定到0.0.0.0,但是
InetAdress.getLocalhost()
返回127.0.1.1(或者localhost,如果这实际上是一个输入错误),这有点不寻常?我认为最有可能的原因是注册表未绑定到192.168.0.104,但您和我都无法确定这一点-只有@Nick可以确定。
registry = 
    LocateRegistry.
        createRegistry( portNumber , 
                        new RMIClientSocketFactory() 
                        {
                            @Override
                            public Socket createSocket( String host, int port ) throws IOException
                            {
                                return new Socket( "127.0.0.1" , port );
                            }
                        } , 
                        new RMIServerSocketFactory() 
                        {
                            @Override
                            public ServerSocket createServerSocket( int port ) throws IOException
                            {
                                return new ServerSocket( port , 0 , InetAddress.getByName( "localhost") );
                            }
                        } );