[英]Java RMI: ConnectException: Connection timed out: connect
I realize this question has been asked many times and answered many times (often from EJP, who clearly knows his stuff!), but I am still struggling. 我知道这个问题已经被问过很多次并回答过很多次了(通常是EJP,他很清楚他的知识!),但是我仍然在努力。
I have "stolen" a simple RMI Adding Server and Client. 我“偷了”一个简单的RMI添加服务器和客户端。 (Thank you, http://www.scs.ryerson.ca/mes/courses/cps530/programs/rmi/Schildt/addTwoNumbers.html who then gives credit to Chapter 24 of "Java 2: The Complete Reference" by P.Naughton and H.Schildt.)
(谢谢您, http://www.scs.ryerson.ca/mes/courses/cps530/programs/rmi/Schildt/addTwoNumbers.html ,然后他将P归功于“ Java 2:完整参考”的第24章。 Naughton和H.Schildt。)
No matter what I try I still get this "Connection timed out" Exception. 无论我尝试什么,我仍然会收到此“连接超时”异常。 Can anyone see what I've done wrong?
谁能看到我做错了什么?
I suspect that there's something going on with the firewall, but if so, I don't know what to tell my IT guys to fix and change. 我怀疑防火墙有什么问题,但是如果是这样,我不知道该告诉我的IT人员如何修复和更改。
Thanks in advance! 提前致谢!
Interface: 接口:
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}
Server: 服务器:
public class SimpleRMIExServer {
public static void main(String[] args) throws Exception {
System.out.println("Class name " + SimpleRMIExServer.class.getSimpleName());
System.out.println("Path " + System.getProperty("user.dir"));
System.setProperty("java.rmi.server.codebase",
"file:///D:\\SimpleRMIExample\\lib\\SimpleRMIExample.jar");
Registry registry;
try {
registry = LocateRegistry.createRegistry(1099);
} catch (RemoteException remoteException) {
registry = LocateRegistry.getRegistry(1099);
}
AddServerImpl addServerImpl = new AddServerImpl();
registry.bind("AddServer", addServerImpl);
System.out.println("Located Registry " + registry.toString());
for (String boundName : registry.list()) {
System.out.println("Name bound: " + boundName);
}
Naming.rebind("SERVERNAME", addServerImpl);
System.out.println("Registry: " + registry);
System.out.println("");
System.out.println("Remote: " + addServerImpl);
}
}
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
public AddServerImpl() throws RemoteException {
}
@Override
public double add(double d1, double d2) throws RemoteException {
return d1 + d2;
}
}
Client: 客户:
public class SimpleRMIExClient {
public static void main(String[] args) throws Exception {
final Registry registry = LocateRegistry.getRegistry("SERVERNAME", 1099);
AddServerIntf addServerIntf = (AddServerIntf) registry.lookup("AddServer");
System.out.println("The first number is: " + 10);
double d1 = Double.valueOf(10).doubleValue();
System.out.println("The second number is: " + 15);
double d2 = Double.valueOf(15).doubleValue();
System.out.println("The sum is: " + addServerIntf.add(d1, d2)); //Exception occurs here.
}
}
Output From Server: 服务器输出:
D:\>java -jar SimpleRMIExServer.jar
Class name SimpleRMIExServer
Path D:\
Located Registry RegistryImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:
1099](local),objID:[0:0:0, 0]]]]
Name bound: AddServer
Registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:1099](l
ocal),objID:[0:0:0, 0]]]]
Remote: AddServerImpl[UnicastServerRef [liveRef: [endpoint:[XX.XX.XX.XX:54119](l
ocal),objID:[2a7202aa:14492e5ea89:-7fff, -5034491972061237368]]]]
Output From Client: 客户输出:
debug:
The first number is: 10
The second number is: 15
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: XX.XX.XX.XX; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
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.invoke(UnicastRef.java:129)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at simplermiexclient.SimpleRMIExClient.main(SimpleRMIExClient.java:35)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 33 seconds)
Additionally, I used the command netstat -a and it showed: 另外,我使用了命令netstat -a,它显示:
LocalAddress ForeignAddress State
0.0.0.0:1099 SERVERNAME:0 LISTENING
[::]:1099 SERVERNAME:0 LISTENING
XX.XX.XX.XX:53373 SERVERNAME:1099 ESTABLISHED
(Among many other lines) I have also seen: (在其他许多行中)我也看到了:
LocalAddress ForeignAddress State
XX.XX.XX.XX:54138 SERVERNAME:1099 TIME_WAIT
Edit: I tried adding in: 编辑:我尝试添加:
System.setProperty("java.rmi.server.hostname", "SERVERNAME");
to the server right after setting the codebase System property, based on EJP's recommendation to so many others to read Java RMI FAQ A.1. 根据EJP向很多其他人的建议,在设置了代码库的System属性后,立即将其连接到服务器以阅读Java RMI FAQ A.1。
When I did, this was the stack trace: 当我这样做时,这是堆栈跟踪:
debug:
The first number is: 10
The second number is: 15
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: SERVERNAME; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
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.invoke(UnicastRef.java:129)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at com.sun.proxy.$Proxy0.add(Unknown Source)
at simplermiexclient.SimpleRMIExClient.main(SimpleRMIExClient.java:35)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 15 seconds)
Answering my own question: I have figured out the answer to my problem. 回答我自己的问题:我已经找到问题的答案。 I love to see others do that, it's good to be able to say it myself.
我喜欢看到别人这样做,很高兴自己能说出来。
My final solution was a firewall problem, but I'd like to provide suggestions to anyone out there for HOW I solved my problem. 我的最终解决方案是防火墙问题,但是我想向所有人提供建议,以帮助我解决问题。
Break it down. 分解。 Take my code above that really isn't my code and make a much simpler version of the RMI server and test it.
将我的代码放在上面,这实际上不是我的代码,并制作一个简单得多的RMI服务器版本并进行测试。
Start small and work your way up. 从小处着手,逐步向上。 Start with localhost make sure that works, then test on a separate PC (I had a virtual PC), then test on your server.
从localhost开始,确保可以正常工作,然后在单独的PC(我有虚拟PC)上进行测试,然后在服务器上进行测试。
GET HELP! 得到帮助! I searched the Internet A LOT.
我在网上搜索了很多。 Thank you, EJP!
谢谢,EJP! Also, once I finally got it working on the Virtual PC, but not on the server, then I go my IT guys involved and they found a difference between the two.
而且,一旦我最终使它在Virtual PC上运行,但在服务器上也无法运行,那么我就让IT人员参与其中,他们发现两者之间存在差异。 "There was a firewall setting on the PC that was related to Java that was not set on the server."
“ PC上有一个与Java相关的防火墙设置,而服务器上没有设置。” - IT Guy (See: http://windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7 )
-IT专家(请参阅: http : //windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7 )
I realize what I've said here is nothing new and I know I've heard it many times, but sometimes it takes hearing it a thousand times to finally accept. 我意识到我在这里所说的并不是什么新鲜事,我知道我已经听过很多次了,但是有时候要听到一千次才能最终接受。 Although it hasn't worked with my kids yet.
虽然还没有和我的孩子一起工作。 ;-)
;-)
Good Luck. 祝好运。
(Answered by a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) ) (由问题编辑回答。转换为社区Wiki答案。请参阅无答案的问题,但问题在注释中已解决(或在聊天中扩展了) )
The OP wrote: OP写道:
Answering my own question: I have figured out the answer to my problem.
回答我自己的问题:我已经找到问题的答案。 I love to see others do that, it's good to be able to say it myself.
我喜欢看到别人这样做,很高兴自己能说出来。
My final solution was a firewall problem, but I'd like to provide suggestions to anyone out there for HOW I solved my problem.
我的最终解决方案是防火墙问题,但是我想向所有人提供建议,以帮助我解决问题。
Break it down.
分解。 Take my code above that really isn't my code and make a much simpler version of the RMI server and test it.
将我的代码放在上面,这实际上不是我的代码,并制作一个简单得多的RMI服务器版本并进行测试。
Start small and work your way up.
从小处着手,逐步向上。 Start with localhost make sure that works, then test on a separate PC (I had a virtual PC), then test on your server.
从localhost开始,确保可以正常工作,然后在单独的PC(我有虚拟PC)上进行测试,然后在服务器上进行测试。
GET HELP!
得到帮助! I searched the Internet A LOT.
我在网上搜索了很多。 Thank you, EJP!
谢谢,EJP! Also, once I finally got it working on the Virtual PC, but not on the server, then I go my IT guys involved and they found a difference between the two.
而且,一旦我最终使它在Virtual PC上运行,但在服务器上也无法运行,那么我就让IT人员参与其中,他们发现两者之间存在差异。 "There was a firewall setting on the PC that was related to Java that was not set on the server."
“ PC上有一个与Java相关的防火墙设置,而服务器上没有设置。” - IT Guy (See: http://windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7 )
-IT专家(请参阅: http : //windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7 )
I realize what I've said here is nothing new and I know I've heard it many times, but sometimes it takes hearing it a thousand times to finally accept.
我意识到我在这里所说的并不是什么新鲜事,我知道我已经听过很多次了,但是有时候要听到一千次才能最终接受。 Although it hasn't worked with my kids yet.
虽然还没有和我的孩子一起工作。 ;-)
;-)
Good Luck.
祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.