java.rmi.ConnectException:拒绝连接主机:192.168.1.8;嵌套异常是:

时间:2015-04-27 10:57:09

标签: java network-programming rmi

我正在尝试基于java RMI创建文件共享应用程序。它抛出一个ConnectException虽然我能够ping通客户端的IP地址。来自彼此的服务器

使用本地主机时整个代码工作正常,但失败了  同时在由2个系统组成的专用网络中做同样的事情。一个 路由器。

FileClientInitialise(服务器)的来源: -

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FileClientInitialise extends Remote{
    public boolean sendData(String fileName, byte[] data, int len) throws RemoteException;
    public String getName() throws RemoteException;
}

FileServerInitialise(服务器)的来源: -

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface FileServerInitialise extends Remote{
    public boolean login(FileClientInitialise fci) throws RemoteException;
}

FileServer(服务器)的来源: -

import java.io.File;
import java.io.FileInputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class FileServer extends UnicastRemoteObject implements FileServerInitialise{

    private String file="";
    protected FileServer() throws RemoteException{
        super();
    }

    public void setFile(String f){
        file=f;
    }

    @Override
    public boolean login(FileClientInitialise fci) throws RemoteException {
        try{
           File f1 = new File(file);
            FileInputStream fis = new FileInputStream(f1);
            byte[] myData = new byte[4096];
            int dataLen = fis.read(myData);
            while(dataLen>0){
                fci.sendData(f1.getName(),myData,dataLen);
                dataLen = fis.read(myData);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }

}

StartFileServer(服务器)的来源: -

    import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class StartFileServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
    System.setProperty("java.rmi.server.hostname","192.168.1.8");
Registry registry=LocateRegistry.createRegistry(1099);
System.out.println("Server Started--- GO,GO,GO--- "+"\nThis Server's IP --- "+InetAddress.getByName("SERVER-1")+"\n");
FileServer fs=new FileServer();
fs.setFile("/root/Downloads/XSS.mp4");  
registry.rebind("asad",fs);
System.out.println("File Server is Ready");
String sa[]=Naming.list("Uploaded");
for(String s: sa){
    System.out.println("Value of s=" + s);
}
}
catch(Exception e){
e.printStackTrace();
}
}   
}

FileClientInitialise.java和。客户端代码相同 FileServerInitialise.java。其余的客户端代码是: -

FileClient.java(客户端)的来源: -

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;

public class FileClient extends UnicastRemoteObject implements FileClientInitialise {
/**
*
*/
private static final long serialVersionUID = 1L;
public String name;
public FileClient (String n) throws RemoteException {
super();
name=n;
}

@Override
public String getName() throws RemoteException{
return name;
}
@Override
public boolean sendData(String filename, byte[] data, int len) throws RemoteException{
try{
File f=new File(filename);
f.createNewFile();
FileOutputStream out=new FileOutputStream(f,true);
out.write(data,0,len);
out.flush();
out.close();
System.out.println("Done writing data...");
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}

StartFileClient.java(客户端)的源代码: -

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;


public class StartFileClient {

public static void main(String[] args) {
try{
Registry registry=LocateRegistry.getRegistry(1099);
FileClient c=new FileClient("imed");
FileServerInitialise server=(FileServerInitialise)registry.lookup("asad");   
//exception being thrown here in above line ---> registry.lookup("asad") method...
server.login(c) ;
System.out.println("Listening.....");   
Scanner s=new Scanner(System.in);   
/*while(true){
String line=s.nextLine();
}*/
}
catch(Exception e){
e.printStackTrace();
}
}   

}

客户端抛出的异常的堆栈跟踪是: -

java.rmi.ConnectException: Connection refused to host: 192.168.1.8;
nested exception is:    java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at
sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at
sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)   at
sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)   at
StartFileClient.main(StartFileClient.java:23)
Caused by: java.net.ConnectException: Connection refused    at     java.net.PlainSocketImpl.socketConnect(Native Method)    at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)   at
java.net.Socket.connect(Socket.java:589)    at
java.net.Socket.connect(Socket.java:538)    at
java.net.Socket.<init>(Socket.java:434)     at
java.net.Socket.<init>(Socket.java:211)     at
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 5 more

1 个答案:

答案 0 :(得分:0)

您需要在服务器中将Registry引用变量设为静态。否则它可能被垃圾收集,最终会导致一切都退出。