开发者

Java Networking: Connection refused - Yes, my server is running

开发者 https://www.devze.com 2023-01-13 22:33 出处:网络
I\'m getting following error when my client tries to connect to my server socket: java.net.ConnectException: Connection refused: connect

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

java.net.ConnectException: Connection refused: connect

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.)

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)

I also tried to completely disable Windows Firewall.

ServerSocket:

public ServerSocket ssocket;
p开发者_Go百科ublic 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


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

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.


Add the java.exe process and the port to your firewall exception list?

edit: Just read you already tried that. All I can suggest is make sure the network is not blocking that port. (routers)


Have You tried running it with JVM option: java.net.preferIPv4Stack=true ?


For what I see in your code, you missed the part where you accept the conection, after instantiating the server socket you need ssocket.accept() to accept conections and then you have to start reading the outputstrem from the socket

0

精彩评论

暂无评论...
验证码 换一张
取 消