Java Server Network Server Error

I am trying to program a (simple, for beginners) server-client, just establish a connection and see if it works. And so it is. As long as I stay inside my four walls / net. As soon as I try to go through the IP addresses of the routers, the client generates a message about a powerful termination. All the usual suspects were eliminated: the router's port forwarding is turned on, the firewall is not interfering (well, it still does not work when I turn it off), and canyouseeme.org says that it can establish a connection to my selected port when the server starts.

Here is the source code for the server, since I realized that you can just go through the command line with a little telnetting. When I try to establish a connection, it just saysCould not open connection to the host, on port 49163:Connection failed

Server:

import java.net.*;
import java.io.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        ServerSocket server = null;

        try {
            System.setProperty("java.net.preferIPv4Stack" , "true");
            server = new ServerSocket(49163);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 49163.");
            System.exit(1);
        }

        Socket client = null;
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        BufferedReader in = new BufferedReader(newInputStreamReader(client.getInputStream()));
        String inputLine;
        String outputLine;

        out.println("Connection established");
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.equals("exit")) {
                break;
            }
        outputLine = inputLine;
        out.println(outputLine);
        }
        out.close();
        in.close();
        client.close();
        server.close();
        System.out.println("Server offline");
    }
}

, , preferIP4vStack -, Stackoverflow, , , .

- , , . , , ( "" IP-), . , :

java.net.ConnectException: Connection refused: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(Unknown Source)
  at java.net.PlainSocketImpl.connectToAddress(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 ChatClient.main(ChatClient.java:12)
+5
2

, . , MAYBE, , , IP- , - . -, , ! .

0

, 49163:

, . Java. , ConnectException, . , , . , , .

, , :

  • 'connection failed', , ,
  • " ", .
  • " ", IP-.
  • " ", , .
0

All Articles