我是意大利人,很抱歉我的英语不好。
我正在用 Java 编写一个 RMI 程序,这是连接到 RMI 服务器的节点代码部分:
try
{//trying to connect to the Server
Remote RemoteObject;
BootstrapServer_Interface serverObject;
Registry r = LocateRegistry.getRegistry(10001);
RemoteObject = r.lookup("BOOTSTRAP-SERVER");//THIS IS LINE 52
serverObject = (BootstrapServer_Interface)RemoteObject;
//I get an ip as Server's answerw
System.out.println("Il nodo "+Thread.currentThread()+" si mette in attesa");
String ipBN = serverObject.start_join_node(fakeip);
if(ipBN.equals("-1"))
{System.out.println("Creazione del nodo "+Thread.currentThread()+" fallito");
return;
}
但是,有时,程序会出现以下异常:
java.rmi.ConnectIOException: Exception creating connection to: 131.114.43.246; nested exception is:
java.net.SocketException: Socket operation on nonsocket: 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 StorageNode.run(StorageNode.java:52)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Socket operation on nonsocket: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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)
... 9 more
这是客户端连接到 RMI 服务器的代码:
try
{//scelgo casualmente un dato da cercare
//genero il mio indirizzo ip
fakeip = String.valueOf((int) (Math.random() * 256)) + "."
+ String.valueOf((int) (Math.random() * 256)) + "."
+ String.valueOf((int) (Math.random() * 256)) + "."
+ String.valueOf((int) (Math.random() * 256));
//creo una Socket UDP con cui contattare il Nodo di Bootstrap
UDPServiceSocket = scanner.UDPSocket();
if(UDPServiceSocket==null)
{System.out.println("Tentativo di creazione socket UDP fallito...");
*return;}
int UDPport = UDPServiceSocket.getLocalPort();
fakeip=fakeip+" UDPport="+UDPport;
//mi connetto al server tramite RMI
Remote RemoteObject;
Registry r = LocateRegistry.getRegistry(10001);
RemoteObject = r.lookup("BOOTSTRAP-SERVER");//THIS IS LINE 48
serverObject = (BootstrapServer_Interface)RemoteObject;
//Ottengo l'ip del Bootstrap Node a cui connettermi
System.out.println("Il client "+Thread.currentThread()+" si mette in attesa.");
ipBN = serverObject.client_search();
}
以及这种情况下的异常(不一定与我上面发布的节点异常一起发生)
java.rmi.ConnectIOException: Exception creating connection to: 131.114.43.246; nested exception is:
java.net.SocketException: Socket operation on nonsocket: 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.run(Client.java:48)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Socket operation on nonsocket: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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)
我找不到导致此异常的任何原因。
我很抱歉我之前发布的代码较少,但它只是一个大项目的一小部分,所以我无法通过所有代码:D
但是,如果您需要它,我可以给您一些有关它的信息:
这是一个 DHT 项目,这是他尝试联系 Bootstrap 服务器(实际上是 RMI 服务器)时的节点代码。
RMI 服务器方法可以被许多节点(上面的代码)和/或许多客户端同时调用。
- 创建节点和/或客户端调用的服务器方法来管理安装在服务器上的监视器。
- 通过这个监视器,节点和客户端按照“写者和读者”的模型(互斥 ecc.)修改一个 Hash Table。
非常感谢所有帮助我解决这个问题的人,再次为我的英语不好感到抱歉!