当我的客户端尝试连接到我的服务器套接字时,出现以下错误:
java.net.ConnectException: Connection refused: connect
但是,我的服务器确实在同一台机器上运行。我尝试使用路由器的外部 IP 连接到它。但是当我尝试连接时"localhost"
, 有用。而且,是的,我在路由器中正确进行了端口转发。甚至canyouseeme.org
可以连接到我的服务器(该网站显示:“成功”,并且在我的服务器日志中显示有人连接到该服务器。)
那么,是否由于某种原因无法通过外部 IP 连接到同一台计算机(或同一网络中的计算机)?或者这是 Windows 的典型特征? (一般情况下我用的是Linux)
我还尝试完全禁用 Windows 防火墙。
服务器套接字:
public ServerSocket ssocket;
public List<ClientHandler> handlers;
public Server(int port) { // Constructor
try {
ssocket = new ServerSocket(port);
this.handlers = new ArrayList<ClientHandler>();
IpSharingManager.uploadData(Utilities.getPublicIp(), port);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
Client:
public InvisibleClient(String host, int port) {
try {
System.out.println("Trying to connect to " + host + ":" + port);
this.host = host;
this.socket = new Socket(host, port);
this.bis = new BufferedInputStream(this.socket.getInputStream());
this.bos = new BufferedOutputStream(this.socket.getOutputStream());
this.console = new RemoteConsole(this.socket);
initializeCommunication();
System.out.println("Successfully connected!");
new Thread(this, "Client Thread").start();
} catch (Exception e) {
e.printStackTrace();
System.out.println("No server available");
}
}
Thanks
有些路由器不允许内部网络连接到路由器的外部IP地址。
您可以尝试使用 telnet 连接到您的服务器套接字。如果 telnet 无法建立连接,则可能是网络问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)