Java网络:连接被拒绝-是的,我的服务器正在运行 [英] Java Networking: Connection refused - Yes, my server is running

查看:261
本文介绍了Java网络:连接被拒绝-是的,我的服务器正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端尝试连接到服务器套接字时出现以下错误:

I'm getting following error when my client tries to connect to my server socket:

java.net.ConnectException: Connection refused: connect

但是,我的服务器确实在同一台机器上运行.我尝试使用路由器的外部IP连接到它.但是,当我尝试连接"localhost" 时,它可以工作.而且,是的,我确实在路由器中正确进行了端口转发.甚至 canyouseeme.org 都可以连接到我的服务器(该站点显示:成功",并且在我的服务器日志中似乎有人连接了该服务器.)

But, my server is really running, on the same machine. I try to connect to it by using the external IP of my router. But when I try to connect with "localhost", it works. And, yes I did port forwarding correcly in my router. Even canyouseeme.org can connect to my server (The site says: "success" and in my server-log appears that someone connected with the server.)

那么,由于一个或另一个原因,是否不可能通过外部IP连接到同一台计算机(或同一网络中的计算机)?或者这是Windows的典型特征?(通常,我使用Linux)

So, is it for one or another reason impossible to connect to the same machine (or to a machine in the same network) via an external IP? Or is this something typical for Windows? (Normally, I use Linux)

我还试图完全禁用Windows防火墙.

I also tried to completely disable Windows Firewall.

ServerSocket:

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);
    }
}

客户:

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");
    }
}

谢谢

推荐答案

某些路由器不允许内部网络连接到路由器的外部IP地址.

Some routers doesn't allow the internal network to connect to the external IP address of the router.

您可以尝试使用telnet连接到服务器套接字.如果telnet无法建立连接,则可能是网络问题.

You can try to use telnet to connect to your server socket. If telnet isn't able to establish a connection, it's likely a networking problem.

这篇关于Java网络:连接被拒绝-是的,我的服务器正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆