[英]Java can not connect to Server Socket
I have a multi threaded Server Socket running on 1 computer running as follows: 我在运行如下的1台计算机上运行多线程服务器套接字:
static void createServer() throws IOException {
//use this ip for other user
System.out.println(InetAddress.getLocalHost());
// establish server socket
try {
ServerSocket s = new ServerSocket(8888);
while (true) {
Socket incoming = s.accept();
Runnable r = new ThreadedEchoHandler(incoming, map);
Thread t = new Thread(r);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} }
Then from another computer I try to connect to the Server(using the ip from first computer 192.168.162.1) as follows: 然后从另一台计算机我尝试连接到服务器(使用第一台计算机192.168.162.1的IP),如下所示:
public void registerCmnd(Scanner keys) throws IOException {
InetAddress ip = InetAddress.getByName("first computer ip");
try (Socket s = new Socket(ip, 8888)) {
.....
.....
}
}
I am getting a java.net.ConnectException. 我正在获取java.net.ConnectException。
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(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 User.registerCmnd(User.java:45)
at User.main(User.java:28)
Any ideas? 有任何想法吗?
It doesn't look like the problem is in the code itself. 看起来问题出在代码本身。
There are a number of possible causes for this, including 有很多可能的原因,包括
A firewall, on the client, the server, a hypervisor stack, or the network is blocking access. 客户端,服务器,虚拟机监控程序堆栈或网络上的防火墙阻止了访问。
You are using the wrong IP address for the server on the client 您为客户端上的服务器使用了错误的IP地址
You are using the wrong server port number on the client (not in this case) 您在客户端上使用了错误的服务器端口号(在这种情况下不是)
You are using an IP address that isn't routed from the client to the server. 您使用的IP地址没有从客户端路由到服务器。 For example, if the server's IP is a private address, and the client is on a different network.
例如,如果服务器的IP是专用地址,而客户端在另一个网络上。
Someone has misconfigured the packet forwarding (eg iptables) or routing (eg routed, etc) on the client or server. 有人错误配置了客户端或服务器上的数据包转发(例如iptables)或路由(例如路由等)。 Or somewhere else.
或者别的地方。
If I were you, I would see whether one computer can PING the other and vice-versa. 如果我是你,我将查看一台计算机是否可以Ping另一台计算机,反之亦然。 If that fails, then check the routing tables.
如果失败,则检查路由表。 Note that this is most likely a network configuration problem, not a programming problem.
请注意,这很可能是网络配置问题,而不是编程问题。
You either have the wrong ip, the port is not forwarded, or both. 您或者输入了错误的IP,或者未转发端口,或者两者都有。 also make sure to run the server first.
还请确保先运行服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.