java 客户端中的 NoRouteToHostException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27221290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoRouteToHostException in Client
提问by RagHaven
I am working on a program that involves a server and a client and I am try to listen on a port and then send messages to that port from a client. However, on doing so I get NoRouteToHostException
. I made a simple Client and Server application to test if I can send and receive messages for my given server and client.
我正在开发一个涉及服务器和客户端的程序,我尝试侦听一个端口,然后从客户端向该端口发送消息。但是,这样做我得到NoRouteToHostException
. 我制作了一个简单的客户端和服务器应用程序来测试我是否可以为给定的服务器和客户端发送和接收消息。
Server:
服务器:
public class Server{
public static void main(String [] args) throws Exception{
ServerSocket s = new ServerSocket(8001);
s.accept();
}
}
Client:
客户:
public class Client{
public static void main(String [] args){
Socket s = new Socket(IP, port);
PrintWriter p = new PrintWriter(s.getOutputStream(), true);
p.println("Hello World");
s.close();
}
}
Exception in thread "main" java.net.NoRouteToHostException: No route to host
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)
I tried to ping the server and it went through as well. Also, if I run the client on the localhost I don't get an exception. I only get the exception when the client is running on a different system.
我尝试 ping 服务器,它也通过了。此外,如果我在本地主机上运行客户端,则不会出现异常。当客户端在不同的系统上运行时,我只会收到异常。
EDITWhen running the server on 8080, it works. I tried allowing all incoming connections to the server by doing iptables --policy INPUT ACCEPT
, but that still doesn't allow me to listen on port 8001
编辑在 8080 上运行服务器时,它可以工作。我尝试通过执行允许所有传入服务器的连接iptables --policy INPUT ACCEPT
,但这仍然不允许我侦听端口 8001
采纳答案by Khanna111
This could only happen, when the port is not accessible because of a firewall in between. This firewall could be anywhere even on the client or the server machine.ping
is to check if the host is accessible which it is but not on port 8001. From ping
, it is confirmed that the host is accessible.
这只会发生在由于中间有防火墙而无法访问端口时。该防火墙甚至可以位于客户端或服务器计算机上的任何位置。ping
是检查主机是否可访问,但在端口 8001 上不可用。从ping
,确认主机可访问。
Try running the server program on another generally open port such as 80, 443, 8080 etc. The network / firewall administrators generally allow these ports to be open and accessible. Note that for port 80, 443 (any port less than 1024) would require root access to bind to it (on the server that is).
尝试在另一个普遍开放的端口上运行服务器程序,例如 80、443、8080 等。网络/防火墙管理员通常允许这些端口开放和访问。请注意,对于端口 80,443(任何小于 1024 的端口)需要 root 访问权限才能绑定到它(在服务器上)。